User Tools

Site Tools


notes:csharp:tuples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

notes:csharp:tuples [2015/06/24]
admin created
notes:csharp:tuples [2016/12/14] (current)
admin
Line 1: Line 1:
 ====== Tuples in C# ====== ====== Tuples in C# ======
  
-Tuples are reference types. ​They can hold sets of elements of different types. They are useful when returning more than one value from a method or with creating collections of value pairs.+They can hold sets of elements of different types. They are useful when returning more than one value from a method or with creating collections of value pairs. ​Tuples are reference types
  
-**Example**: Instantiate ​tuples:+Example: Instantiate ​a tuple:
 <code csharp> <code csharp>
 // via constructor // via constructor
Line 13: Line 13:
  
  
-**Example**: Access properties of a tuple:+Example: Access properties of a tuple:
 <code csharp> <code csharp>
 var t = new Tuple<​int,​ string>​(256,​ "​Test"​);​ var t = new Tuple<​int,​ string>​(256,​ "​Test"​);​
Line 22: Line 22:
  
  
-**Example**: Compare tuples (the //Equals// method is overridden to compare individual elements):+Example: Compare tuples (the //Equals// method is overridden to compare individual elements):
 <code csharp> <code csharp>
 var t1 = Tuple.Create(256,​ "​Test1"​);​ var t1 = Tuple.Create(256,​ "​Test1"​);​
Line 31: Line 31:
  
  
-**Example**: Return Tuple<​int,​ int> from a method:+Example: Return Tuple<​int,​ int> from a method:
 <code csharp> <code csharp>
 public Tuple<​int,​int>​ Divide(int dividend, int divisor) public Tuple<​int,​int>​ Divide(int dividend, int divisor)
Line 40: Line 40:
     return Tuple.Create<​int,​int>​(result,​ reminder);     return Tuple.Create<​int,​int>​(result,​ reminder);
 } }
- 
 ... ...
- 
 Tuple<​int,​ int> n = Divide(7, 3); Tuple<​int,​ int> n = Divide(7, 3);
-Console.WriteLine("​result:​ {0}, reminder: {1}", n.Item1, ​n.Item2); // result: 2, reminder: 1+Console.WriteLine($"​result:​ {n.Item1}, reminder: {n.Item2}"); // result: 2, reminder: 1
 </​code>​ </​code>​
  
  
-**Example**: Create a tuple with more than eight parameters by passing a tuple as the last template parameter (called //TRest//). This technique can be used to create a tuple with any number of items:+Example: Create a tuple with more than eight parameters by passing a tuple as the last template parameter (called //TRest//). This technique can be used to create a tuple with any number of items:
 <code csharp> <code csharp>
 var tuple = Tuple.Create<​string,​ string, string, int, int, int, double, Tuple<​int,​int>>​( var tuple = Tuple.Create<​string,​ string, string, int, int, int, double, Tuple<​int,​int>>​(
notes/csharp/tuples.txt ยท Last modified: 2016/12/14 by admin