- C# 7 and .NET Core 2.0 High Performance
- Ovais Mehboob Ahmed Khan
- 263字
- 2021-08-27 18:47:10
Tuples
Tuples solve the problem of returning more than one value from a method. Traditionally, we can use out variables that are reference variables, and the value is changed if they are modified from the calling method. However, without parameters, there are some limitations, such as that it cannot be used with async methods and is not recommended to be used with external services.
Tuples have the following characteristics:
- They are value types.
- They can be converted to other Tuples.
- Tuple elements are public and mutable.
A Tuple is represented as System.Tuple<T>, where T could be any type. The following example shows how a Tuple can be used with the method and how the values can be invoked:
static void Main(string[] args) { var person = GetPerson(); Console.WriteLine($"ID : {person.Item1},
Name : {person.Item2}, DOB : {person.Item3}"); } static (int, string, DateTime) GetPerson() { return (1, "Mark Thompson", new DateTime(1970, 8, 11)); }
As you may have noticed, items are dynamically named and the first item is named Item1, the second Item2, and so on. On the other hand, we can also name the items so that the calling party should know about the value, and this can be done by adding the parameter name for each parameter in the Tuple, which is shown as follows:
static void Main(string[] args) { var person = GetPerson(); Console.WriteLine($"ID : {person.id}, Name : {person.name},
DOB : {person.dob}"); } static (int id, string name, DateTime dob) GetPerson() { return (1, "Mark Thompson", new DateTime(1970, 8, 11)); }
https://docs.microsoft.com/en-us/dotnet/csharp/tuples.
- Microsoft SQL Server企業級平臺管理實踐
- 企業大數據系統構建實戰:技術、架構、實施與應用
- 云計算與大數據應用
- Sybase數據庫在UNIX、Windows上的實施和管理
- 數字媒體交互設計(初級):Web產品交互設計方法與案例
- 跟老男孩學Linux運維:MySQL入門與提高實踐
- Proxmox VE超融合集群實踐真傳
- gnuplot Cookbook
- 重復數據刪除技術:面向大數據管理的縮減技術
- Google Cloud Platform for Developers
- 數字IC設計入門(微課視頻版)
- Mastering ROS for Robotics Programming(Second Edition)
- Oracle 11g+ASP.NET數據庫系統開發案例教程
- 實現領域驅動設計
- openGauss數據庫核心技術