Skip to content

Commit 07063e7

Browse files
committed
implemented Deconstruct using an extension method
1 parent 60f251f commit 07063e7

6 files changed

Lines changed: 136 additions & 25 deletions

File tree

.vscode/launch.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/csharp-deconstruct.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false
19+
},
20+
{
21+
"name": ".NET Core Attach",
22+
"type": "coreclr",
23+
"request": "attach"
24+
}
25+
]
26+
}

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/csharp-deconstruct.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/csharp-deconstruct.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/csharp-deconstruct.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

Extensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CSharp.Deconstruction.Demo
2+
{
3+
public static class Extensions
4+
{
5+
public static void Deconstruct(this DateTimeOffset date, out int day, out int month, out int year) =>
6+
(day, month, year) = (date.Day, date.Month, date.Year);
7+
}
8+
}

MyBook.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
// declare a class with Deconstruct method(s)
2-
class MyBook
1+
namespace CSharp.Deconstruction.Demo
32
{
4-
public int Id {get; set;}
5-
public string Title {get; set;}
6-
public string Author {get; set;}
7-
8-
public MyBook(int id, string title, string author)
3+
// declare a class with Deconstruct method(s)
4+
class MyBook
95
{
10-
Id = id;
11-
Title = title;
12-
Author = author;
13-
}
6+
public int Id { get; set; }
7+
public string Title { get; set; }
8+
public string Author { get; set; }
149

15-
public void Deconstruct(out int id, out string title, out string author)
16-
{
17-
id = Id;
18-
title = Title;
19-
author = Author;
20-
}
10+
public MyBook(int id, string title, string author)
11+
{
12+
Id = id;
13+
Title = title;
14+
Author = author;
15+
}
2116

22-
public void Deconstruct(out string title, out string author)
23-
{
24-
title = Title;
25-
author = Author;
17+
public void Deconstruct(out int id, out string title, out string author)
18+
{
19+
id = Id;
20+
title = Title;
21+
author = Author;
22+
}
23+
24+
public void Deconstruct(out string title, out string author)
25+
{
26+
title = Title;
27+
author = Author;
28+
}
2629
}
2730
}

Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,23 @@ public static void Main()
6060

6161
// instantitate a Person record
6262
var person = new Person("Jon", "Doe");
63+
64+
// deconstruct person record
6365
var (firstName, lastName) = person;
6466
Console.WriteLine($"My name is {firstName} {lastName}");
67+
68+
69+
/*******************************************
70+
* Example: Deconstruction using extension method
71+
*******************************************/
72+
73+
// implemeted an extension method in "Extensions.cs" file
74+
// now instantitate DateTimeOffset
75+
var date = new DateTimeOffset(2022, 9, 17, 0, 0, 0, 0, TimeSpan.Zero);
76+
77+
// deconstruct DateTimeOffset objcet
78+
(int day, int month, int year) = date;
79+
Console.WriteLine($"I wrote this example on: {month}/{day}/{year}");
6580
}
6681
}
6782
}

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Let's learn more about this process by looking at the following code examples.
55

6-
## Example: Deconstruct a tuple
6+
## Example: Deconstruction of a tuple object
77

88
```csharp
99
// declare a tuple
@@ -19,7 +19,7 @@ Console.WriteLine($"Price: {price}");
1919

2020
```
2121

22-
## Example: Deconstruct a dictionary
22+
## Example: Deconstruction of a dictionary object
2323

2424
```csharp
2525
// declare a dictionary object
@@ -34,7 +34,7 @@ foreach((string k, string v) in books)
3434
Console.WriteLine($"\"{k}\" written by {v}");
3535
}
3636
```
37-
## Example: Deconstruct a class
37+
## Example: Deconstruction of a class
3838
We need to implement the `Deconstruct` method. We can have multiple implementation of this method by overloading.
3939

4040
```csharp
@@ -82,7 +82,7 @@ var (myBookTitle, myBookAuthor) = mybook;
8282
Console.WriteLine($"\"{myBookTitle}\" by {myBookAuthor}");
8383
```
8484

85-
## Example: Deconstruct a record
85+
## Example: Deconstruction of a record
8686

8787
```csharp
8888
// define a record called "Person" with the props
@@ -94,6 +94,24 @@ var person = new Person("Jon", "Doe");
9494
var (firstName, lastName) = person;
9595
Console.WriteLine($"My name is {firstName} {lastName}");
9696
```
97+
98+
## Example: Deconstruction using extension method
99+
100+
```csharp
101+
// implemeted an extension method
102+
public static void Deconstruct(this DateTimeOffset date, out int day, out int month, out int year) =>
103+
(day, month, year) = (date.Day, date.Month, date.Year);
104+
```
105+
106+
```csharp
107+
// now instantitate DateTimeOffset
108+
var date = new DateTimeOffset(2022, 9, 17, 0, 0, 0, 0, TimeSpan.Zero);
109+
110+
// deconstruct DateTimeOffset objcet
111+
(int day, int month, int year) = date;
112+
Console.WriteLine($"I wrote this example on: {month}/{day}/{year}");
113+
```
114+
97115
---
98116
## Links:
99117
- [Deconstructing tuples and other types](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct)

0 commit comments

Comments
 (0)