-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTupleDeconstruction.cs
68 lines (59 loc) · 2.36 KB
/
TupleDeconstruction.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
57
58
59
60
61
62
63
64
65
66
67
68
using System;
namespace CodeSamples.TupleDeconstruction
{
#region Worker Class
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public void Deconstruct(out string firstName, out string lastName)
{
firstName = FirstName;
lastName = LastName;
}
public void Deconstruct(out string firstName, out string lastName, out int age)
{
firstName = FirstName;
lastName = LastName;
age = Age;
}
public (string, int) GetNameAndAge()
{
return ($"{FirstName} {LastName}", Age);
}
public (string fullName, int age) GetNameAndAgeNamed()
{
return ($"{FirstName} {LastName}", Age);
}
}
#endregion
public class TupleDeconstructionSample : SampleExecute
{
public override void Execute()
{
var user = new User()
{
FirstName = "Name",
LastName = "LastName",
Email = "[email protected]",
Age = 42
};
(var firstName1, var lastName1) = user; //using Deconstruct method
(var firstName2, var lastName2, var age) = user; //using Deconstruct method
var unnamedTuple = user.GetNameAndAge();
var namedTuple = user.GetNameAndAgeNamed();
(string tupleFullNameTyped, int tupleAgeTyped) = user.GetNameAndAge();
var (tupleFullNameVar, tupleAgeVar) = user.GetNameAndAge();
Title("TupleDeconstructionExecute");
Console.WriteLine($"First deconstruction: First Name = {firstName1}, Last Name = {lastName1}");
Console.WriteLine($"Second deconstruction: First Name = {firstName2}, Last Name = {lastName2}, Age = {age}");
Console.WriteLine($"Unnamed Tuple: Name = {unnamedTuple.Item1}, Age = {unnamedTuple.Item2}");
Console.WriteLine($"Named Tuple: Name = {namedTuple.fullName}, Age = {namedTuple.age}");
Console.WriteLine($"Typed Params: Name = {tupleFullNameTyped}, Age = {tupleAgeTyped}");
Console.WriteLine($"Untyped (var) Params: Name = {tupleFullNameVar}, Age = {tupleAgeVar}");
Finish();
}
}
}