- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 419字
- 2021-07-03 00:11:53
How to do it...
- If you created the Student class earlier, you should have something similar to this in your code:
public class Student
{
public string Name { get; set; }
public string LastName { get; set; }
public List<int> CourseCodes { get; set; }
}
- To create a deconstructor, add a Deconstruct method to your Student class. You will notice that this is a void method that takes two out parameters (in this instance). We then just assign the values of Name and LastName to the out parameters.
If we wanted to deconstruct more values in the Student class, we would pass in more out parameters, one for each value we wanted to deconstruct.
public void Deconstruct(out string name, out string lastName)
{
name = Name;
lastName = LastName;
}
- Your modified Student class should now look as follows:
public class Student
{
public string Name { get; set; }
public string LastName { get; set; }
public List<int> CourseCodes { get; set; }
public void Deconstruct(out string name, out string lastName)
{
name = Name;
lastName = LastName;
}
}
- Consuming our Student class (just like we did with Tuples) can now be accomplished as follows:
Student student = new Student();
student.Name = "Dirk";
student.LastName = "Strauss";
var (FirstName, Surname) = student;
WriteLine($"The student name is {FirstName} {Surname}");
- Running the Console Application will display the deconstructed values returned from the Student class.

- Deconstructors can just as easily be used in extension methods. This is quite a nice way to extend the existing type to include a deconstruction declaration. To implement this, we need to remove the deconstructor from our Student class. You can just comment it out for now, but essentially this is what we are after:
public class Student
{
public string Name { get; set; }
public string LastName { get; set; }
public List<int> CourseCodes { get; set; }
}
- The Student class now does not contain a deconstructor. Head on over to the extension methods class and add the following extension method:
public static void Deconstruct(this Student student,
out string firstItem, out string secondItem)
{
firstItem = student.Name;
secondItem = student.LastName;
}
- The extension method acts on a Student type only. It follows the same basic implementation of the deconstructor created earlier in the Student class itself. Running the console application again, you will see the same result as before. The only difference is that the code is now using the extension method to deconstruct values in the Student class.

推薦閱讀
- 大話PLC(輕松動漫版)
- 高效微控制器C語言編程
- Python深度學習
- Oracle 12c中文版數據庫管理、應用與開發實踐教程 (清華電腦學堂)
- 深入理解Java7:核心技術與最佳實踐
- Learn React with TypeScript 3
- Learning Apache Mahout Classification
- 零基礎輕松學SQL Server 2016
- Cybersecurity Attacks:Red Team Strategies
- Mastering C++ Multithreading
- C++編程兵書
- Python Social Media Analytics
- Web前端測試與集成:Jasmine/Selenium/Protractor/Jenkins的最佳實踐
- Zend Framework 2 Cookbook
- PHP 7 Programming Blueprints