- Mastering Visual Studio 2017
- Kunal Chowdhury
- 517字
- 2021-07-15 17:26:38
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:

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.
- Tuples are value types
- Elements of tuples are public and mutable
- Two tuples are equal, if their elements are pairwise equal
- 多媒體CAI課件設計與制作導論(第二版)
- Spring Boot 2實戰之旅
- Instant Apache Stanbol
- Java EE框架整合開發入門到實戰:Spring+Spring MVC+MyBatis(微課版)
- DevOps入門與實踐
- TypeScript實戰指南
- 編譯系統透視:圖解編譯原理
- ASP.NET 3.5程序設計與項目實踐
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- SQL Server 2016數據庫應用與開發習題解答與上機指導
- Mastering Linux Security and Hardening
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- Groovy 2 Cookbook
- Instant GLEW
- Python應用與實戰