-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
82 lines (62 loc) · 2.89 KB
/
Program.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
namespace CSharp.Deconstruction.Demo
{
// define a record called "Person" with the props
public record Person(string FirstName, string LastName);
public class Program
{
public static void Main()
{
/*******************************************
* Example: Deconstruction of a tuple object
*******************************************/
// declare a tuple
var book = ("C-Sharp Basics", "Jon Doe", 1.99);
// now we can destruct the tuples into individual items
var (title, author, price) = book;
// use them like variables
Console.WriteLine($"Title: {title}");
Console.WriteLine($"Author: {author}");
Console.WriteLine($"Price: {price}");
/*******************************************
* Example: Deconstruction of a dictionary object
*******************************************/
// declare a dictionary object
var books = new Dictionary<string, string>
{
{"Book One", "Author One"},
{"Book Two", "Author Two"},
};
// unpack each item into a key/value pair (title/author pair) and loop through
foreach ((string k, string v) in books)
{
Console.WriteLine($"\"{k}\" written by {v}");
}
/*******************************************
* Example: Deconstruction of a class
*******************************************/
// instantiate the "MyBook" class
var mybook = new MyBook(1, "CSharp Bacis", "Jon Doe");
// deconstruct class properties into a collection of variables
var (myBookTitle, myBookAuthor) = mybook;
// use the variables as needed
Console.WriteLine($"\"{myBookTitle}\" by {myBookAuthor}");
/*******************************************
* Example: Deconstruction of a record
*******************************************/
// instantitate a Person record
var person = new Person("Jon", "Doe");
// deconstruct person record
var (firstName, lastName) = person;
Console.WriteLine($"My name is {firstName} {lastName}");
/*******************************************
* Example: Deconstruction using extension method
*******************************************/
// implemeted an extension method in "Extensions.cs" file
// now instantitate DateTimeOffset
var date = new DateTimeOffset(2022, 9, 17, 0, 0, 0, 0, TimeSpan.Zero);
// deconstruct DateTimeOffset objcet
(int day, int month, int year) = date;
Console.WriteLine($"I wrote this example on: {month}/{day}/{year}");
}
}
}