官术网_书友最值得收藏!

New changes to tuples

A tuple is a finite ordered list of elements. In C#, it's not a new thing, having been available since .NET Framework 4.0. It is useful when you want to return multiple values from a method without creating a separate class or collection to hold the data. Tuples are declared in the manner, Tuple<T1, T2, T3, ...> and are available under the System namespace.

By default, a tuple can hold up to eight value types, but you can extend it by adding the reference of System.Runtime.

Although this is useful when returning multiple values, you need to create an allocation for the System.Tuple<...> object. The following example demonstrates how to return multiple values using a tuple:

   public static Tuple<string, string> GetAuthor() 
   { 
     return new Tuple<string, string>("Kunal Chowdhury", 
                                      "www.kunal-chowdhury.com"); 
   } 

Based on the number of elements, the returned value generates properties dynamically to get the individual values. For example, if you return two values using a tuple, the returned object will have Item1 and Item2 properties to access the individual values. If the tuple object has three values, the properties will be Item1, Item2, Item3, and Item4.

Here you can see an example of how to access the returned values:

In C# 7.0, you don't need to specify it by explicitly creating the tuple object. Rather, you can specify the return types within brackets, as shown in the following code snippet:

    public static (string, string) GetAuthor() 
    { 
      return ("Kunal Chowdhury", "www.kunal-chowdhury.com"); 
    } 

If you receive an error using the new way of using tuples in C# 7.0, you must explicitly reference System.ValueTuple from the NuGet package library. To install the package, either open the NuGet package manager or the NuGet package manager console; or you can simply click the Install package 'System.ValueTuple' menu item from the tooltip, as shown in the following screenshot. This is the simplest way to download and install the package:

Alternatively, you can find the package here: http://www.nuget.org/packages/System.ValueTuple/.

Once you have installed the package, you can see that the DLL reference to the System.ValueTuple has already been added to your project:

Apart from the preceding simplest way, C# 7.0 also allows you to define the name to the tuple elements to describe them easily:

    public static (string AuthorName, string Blog) GetAuthor() 
    { 
      return ("Kunal Chowdhury", "www.kunal-chowdhury.com"); 
    } 

You can also specify the element names directly to the tuple literals, making it simpler and error free while assigning:

    public static (string AuthorName, string Blog) GetAuthor() 
    { 
      return (AuthorName: "Kunal Chowdhury", 
              Blog: "www.kunal-chowdhury.com"); 
    } 

Setting a descriptive name to the tuple element makes it easily identifiable while accessing from the returned object. In the preceding example, AuthorName and Blog are better than Item1 and Item2.

When you make the changes to your existing tuple definition, you can easily rename it in the accessible method by following the step given here:

When you are directly using tuples in Visual Studio editor window, you can see them appear in IntelliSense to use in your code.

Points to remember:
  • Tuples are value types
  • Elements of tuples are public and mutable
  • Two tuples are equal, if their elements are pairwise equal
主站蜘蛛池模板: 宜丰县| 昆山市| 娄烦县| 即墨市| 新兴县| 连州市| 安龙县| 河西区| 元谋县| 镇平县| 辽中县| 新竹市| 凤庆县| 洛阳市| 镶黄旗| 双流县| 淳安县| 社会| 岳阳市| 广宗县| 蕉岭县| 无锡市| 梁河县| 宁河县| 曲阜市| 西乡县| 青岛市| 红安县| 迁西县| 东阳市| 惠来县| 仙桃市| 海门市| 衡山县| 偃师市| 莆田市| 南召县| 莲花县| 晋中市| 潮州市| 重庆市|