- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
82 lines (62 loc) · 2.89 KB
/
Copy pathProgram.cs
File metadata and controls
82 lines (62 loc) · 2.89 KB
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
namespaceCSharp.Deconstruction.Demo
{
// define a record called "Person" with the props
publicrecordPerson(stringFirstName,stringLastName);
publicclassProgram
{
publicstaticvoidMain()
{
/*******************************************
* Example: Deconstruction of a tuple object
*******************************************/
// declare a tuple
varbook=("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
varbooks=newDictionary<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((stringk,stringv)inbooks)
{
Console.WriteLine($"\"{k}\" written by {v}");
}
/*******************************************
* Example: Deconstruction of a class
*******************************************/
// instantiate the "MyBook" class
varmybook=newMyBook(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
varperson=newPerson("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
vardate=newDateTimeOffset(2022,9,17,0,0,0,0,TimeSpan.Zero);
// deconstruct DateTimeOffset objcet
(intday,intmonth,intyear)=date;
Console.WriteLine($"I wrote this example on: {month}/{day}/{year}");
}
}
}