-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01Tuples.cs
56 lines (46 loc) · 1.8 KB
/
01Tuples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
namespace CSharp7Console
{
public class Tuples
{
//Currently tuples are ref types, and immutable, meaning each time you change a value a new object is created
//this makes them very inefficient, but thread safe.
//New Tuples are value types, making them still thread safe, but now more efficient.
private static Tuple<int, string> GetEmployeeByIdExisting(int employeeId)
{
var empAge = 32;
var dept = "HR";
return new Tuple<int, string>(empAge, dept);
}
//Shortened syntax for the tuple
private static (int, string) GetEmployeeByIdShortenedSyntax(int employeeId)
{
var empAge = 32;
var dept = "HR";
return (empAge, dept);
}
//Shortened named fields in the tuple
private static (int age, string department) GetEmployeeByIdNamedFields(int employeeId)
{
var empAge = 32;
var dept = "HR";
return (empAge, dept);
}
public static void TupleExample()
{
var employeeTuple = GetEmployeeByIdExisting(1);
var age = employeeTuple.Item1;
var dept = employeeTuple.Item2;
//newer approach with shortened syntax..
var employeeShortSyntaxTuple = GetEmployeeByIdShortenedSyntax(1);
age = employeeShortSyntaxTuple.Item1;
dept = employeeShortSyntaxTuple.Item2;
//newer approach with named fields..
var employee = GetEmployeeByIdNamedFields(1);
age = employee.age;
dept = employee.department; //bug reverts to items2
dept = employee.Item2; //intelli sense is screwed up..
Console.WriteLine($"{age} {dept}");
}
}
}