Hello.
In this lecture, we're going to talk about interview question number 12.
What is the construction?
The construction is a mechanism that allows breaking it up into individual variables.
It is also possible to define how the construction should work for use of different types by implementing
the deconstruct method.
The construction was first introduced with C-sharp seven.
First, let's see some code.
This method takes a collection of integers and returns the sum count and average as a free element vote
pool.
Now, let's see how this method could be used.
Here we simply executed this method and use its result in all those places.
Since we use each of the Topos elements quite often, let's throw them in variables.
This works, but it's a bit cumbersome.
It would be better if we could create those free variables in the same line.
The Analyze Numbers method is executed.
And that's exactly what the construction is for.
Let's see this in gold.
In this line, we declared three variables and assigned the first element of the tadpole to the first
one, the second to the second one and the first to the third one.
The count of variables must be equal to the count of double elements.
So, for example, this will not work.
But we don't need to declare as a variable if we don't need it.
Let's say that for some reason, I don't care about the second taboo element, which is the count.
I can skip it by using the this card.
This card is a special right only variable, and we can't use it after it's assigned.
Its only purpose is to be a placeholder for inert elements of a typo.
It is also possible to deconstruct taps into variables that we already have.
In this case, we just need to skip the VAR keyword.
We can also mix using the existing variables with declaring new ones.
All right.
So far, we've been deconstructing married couples.
We can also deconstruct ordinary typos.
We can also deconstruct positional records, which we will learn about later in the course.
Now let's define our own custom glass.
And here it is, a class representing a classes by default, do not support being deconstructed.
As you can see here, we see a compilation error, but we can provide our own deconstruct method to
enable it.
Such a method must be void and it must have one out parameter for each variable that will be created
as the result.
Let's add the deconstruct method to the glass.
And now we are able to deconstruct the pet object into three variables.
We can define as many deconstruct methods in a class as we want.
We can also add the deconstruct method to structs records and interfaces.
Even if we did not create some grass and we don't have access to its source code, we can still add
the deconstruct method to it.
Using extension methods, let's see this in practice.
Let's say I wished I could deconstruct an object.
Unfortunately, this doesn't work because Town does not have the deconstruct method implemented.
Let's fix it by defining the deconstruct extension method.
And now the deconstruction works as expected.
All right.
So we learned that the construction is a mechanism that allows breaking a taboo into variables.
We can also define that the construction for our custom crosses during the interview.
You might be asked what is the difference between the destructor and the deconstruct method?
Well, they are completely different things.
The destructor is a method that's called on an object when it is being removed from memory by the garbage
collector.
The deconstruct method allows the object to be deconstructed into single variables.
It is by default generated for Topos value taboos and positional records, but we can also define it
in custom types.
How can we define the construction, the four types that we did not create, and we don't have access
to the source code.
We can define that deconstruct method as an extension method for this type.
All right, that's it about deconstruction.
Thanks for watching and see you in the next video.