Friday 4 April 2014

C# Tuple Class

The Tuple class is a new feature of ASP .NET 4.0 that enables you to very quickly create custom data structures similar to classes. Tuple objects can't contain methods, they are only structures for data storage. It is best practice to avoid using Tuple as where possible as they don't have the flexibility of real classes and can make your code harder to understand.

Tuple objects offer an easy way to return multiple values from a method. It is important to note however that it is better to do this using either the out keyword or ideally returning a custom class.
Tulple objects have a limit of eight properties, although you can nest Tuple objects by using Tuple as a property type within another Tuple. You need to set the values of a Tuple object's properties when you create it. After the Tuple object has been created it's values will be read only.
Once a Tuple object is created, it will behave in exactly the same way as any other object that has read-only properties.

As well as creating a Tuple object using the standard method of instantiating a class, you can also use the Tuple.Create method. The only reason to use this method is to cut down on the amount of code you need to type as the Create method will automatically select the correct data types for the values you provide.

No comments:

Post a Comment