Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C# 7 and .NET Core Cookbook

You're reading from   C# 7 and .NET Core Cookbook Serverless programming, Microservices and more

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher
ISBN-13 9781787286276
Length 628 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dirk Strauss Dirk Strauss
Author Profile Icon Dirk Strauss
Dirk Strauss
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. New Features in C# 7.0 FREE CHAPTER 2. Classes and Generics 3. Object-Oriented Programming in C# 4. Code Analyzers in Visual Studio 5. Regular Expressions 6. Working with Files, Streams, and Serialization 7. Making Apps Responsive with Asynchronous Programming 8. High Performance Programming Using Parallel and Multithreading in C# 9. Composing Event-Based Programs Using Reactive Extensions 10. Exploring .NET Core 1.1 11. ASP.NET Core on the MVC Framework 12. Choosing and Using a Source Control Strategy 13. Creating a Mobile Application in Visual Studio 14. Writing Secure Code and Debugging in Visual Studio 15. Creating Microservices on Azure Service Fabric 16. Azure and Serverless Computing

Deconstruction

Tuples can be consumed using a deconstruction declaration. This simply splits a Tuple into its individual parts and assigns these parts to new variables. This is called deconstruction, and it is not only reserved for Tuples.

Getting ready

Remember when we used Tuples at the beginning of this chapter? Well we were using code similar to the following to get the values returned by the Tuple literal.

var (average, studentCount) = ch1.GetAverageAndCount(scores);

This was deconstructing the parts of the Tuple into the new variables average and studentCount. I do not, however, want to take a look at Tuples again. What I want to do is show how you can implement a deconstruction declaration on any type. To do this, all that we need to do is ensure that the type has a deconstructor method. We will modify our existing Student class to add a deconstructor.

How to do it...

  1. 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; }
}
  1. 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;
}
  1. 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;
}
}
  1. 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}");
  1. Running the Console Application will display the deconstructed values returned from the Student class.
  1. 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; }
}
  1. 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;
}
  1. 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.

How it works...

In the code example, we set the student name and last name to specific values. This was just to illustrate the use of deconstruction. A more likely scenario would be to pass a student number to the Student class (in the constructor perhaps), as follows:

Student student = new Student(studentNumber);

The implementation within the Student class would then perform a database lookup using the student number passed through in the constructor. This will then return the student details. A more likely implementation of the Student class would probably look as follows:

public class Student
{
public Student(string studentNumber)
{
(Name, LastName) = GetStudentDetails(studentNumber);
}
public string Name { get; private set; }
public string LastName { get; private set; }
public List<int> CourseCodes { get; private set; }

public void Deconstruct(out string name, out string lastName)
{
name = Name;
lastName = LastName;
}

private (string name, string surname) GetStudentDetails(string studentNumber)
{
var detail = (n: "Dirk", s: "Strauss");
// Do something with student number to return the student details
return detail;
}
}

You will notice that the GetStudentDetails() method is just a dummy implementation. This is where the database lookup will start and the values will be returned from here. The code that calls the Student class now makes more sense. We call the Student class, pass it a student number, and deconstruct it to find the student's first name and surname.

Student student = new Student("S20323742");
var (FirstName, Surname) = student;
WriteLine($"The student name is {FirstName} {Surname}");
You have been reading a chapter from
C# 7 and .NET Core Cookbook - Second Edition
Published in: Apr 2017
Publisher:
ISBN-13: 9781787286276
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime