1 00:00:06,690 --> 00:00:07,070 Hello. 2 00:00:07,860 --> 00:00:12,210 In this lecture, we're going to talk about interview question number 12. 3 00:00:12,510 --> 00:00:14,100 What is the construction? 4 00:00:14,670 --> 00:00:20,310 The construction is a mechanism that allows breaking it up into individual variables. 5 00:00:20,610 --> 00:00:27,300 It is also possible to define how the construction should work for use of different types by implementing 6 00:00:27,300 --> 00:00:28,800 the deconstruct method. 7 00:00:29,370 --> 00:00:33,090 The construction was first introduced with C-sharp seven. 8 00:00:33,510 --> 00:00:35,580 First, let's see some code. 9 00:00:36,360 --> 00:00:44,610 This method takes a collection of integers and returns the sum count and average as a free element vote 10 00:00:44,670 --> 00:00:44,980 pool. 11 00:00:45,450 --> 00:00:48,510 Now, let's see how this method could be used. 12 00:00:49,500 --> 00:00:54,540 Here we simply executed this method and use its result in all those places. 13 00:00:55,080 --> 00:01:00,480 Since we use each of the Topos elements quite often, let's throw them in variables. 14 00:01:15,880 --> 00:01:18,250 This works, but it's a bit cumbersome. 15 00:01:18,670 --> 00:01:22,960 It would be better if we could create those free variables in the same line. 16 00:01:23,010 --> 00:01:25,750 The Analyze Numbers method is executed. 17 00:01:26,050 --> 00:01:29,050 And that's exactly what the construction is for. 18 00:01:29,320 --> 00:01:30,930 Let's see this in gold. 19 00:01:35,170 --> 00:01:41,110 In this line, we declared three variables and assigned the first element of the tadpole to the first 20 00:01:41,110 --> 00:01:45,190 one, the second to the second one and the first to the third one. 21 00:01:45,730 --> 00:01:49,810 The count of variables must be equal to the count of double elements. 22 00:01:50,020 --> 00:01:52,570 So, for example, this will not work. 23 00:01:54,700 --> 00:01:58,330 But we don't need to declare as a variable if we don't need it. 24 00:01:58,570 --> 00:02:04,390 Let's say that for some reason, I don't care about the second taboo element, which is the count. 25 00:02:04,690 --> 00:02:07,040 I can skip it by using the this card. 26 00:02:09,160 --> 00:02:14,950 This card is a special right only variable, and we can't use it after it's assigned. 27 00:02:15,310 --> 00:02:19,630 Its only purpose is to be a placeholder for inert elements of a typo. 28 00:02:20,110 --> 00:02:24,790 It is also possible to deconstruct taps into variables that we already have. 29 00:02:25,270 --> 00:02:28,480 In this case, we just need to skip the VAR keyword. 30 00:02:32,620 --> 00:02:37,510 We can also mix using the existing variables with declaring new ones. 31 00:02:40,840 --> 00:02:41,450 All right. 32 00:02:41,620 --> 00:02:44,740 So far, we've been deconstructing married couples. 33 00:02:44,980 --> 00:02:47,680 We can also deconstruct ordinary typos. 34 00:02:58,590 --> 00:03:04,230 We can also deconstruct positional records, which we will learn about later in the course. 35 00:03:04,920 --> 00:03:08,190 Now let's define our own custom glass. 36 00:03:09,600 --> 00:03:17,010 And here it is, a class representing a classes by default, do not support being deconstructed. 37 00:03:28,840 --> 00:03:35,620 As you can see here, we see a compilation error, but we can provide our own deconstruct method to 38 00:03:35,620 --> 00:03:36,400 enable it. 39 00:03:37,120 --> 00:03:44,050 Such a method must be void and it must have one out parameter for each variable that will be created 40 00:03:44,050 --> 00:03:44,980 as the result. 41 00:03:45,310 --> 00:03:48,370 Let's add the deconstruct method to the glass. 42 00:04:04,670 --> 00:04:10,430 And now we are able to deconstruct the pet object into three variables. 43 00:04:10,850 --> 00:04:15,200 We can define as many deconstruct methods in a class as we want. 44 00:04:15,740 --> 00:04:21,140 We can also add the deconstruct method to structs records and interfaces. 45 00:04:21,740 --> 00:04:28,070 Even if we did not create some grass and we don't have access to its source code, we can still add 46 00:04:28,070 --> 00:04:29,720 the deconstruct method to it. 47 00:04:29,840 --> 00:04:33,470 Using extension methods, let's see this in practice. 48 00:04:33,920 --> 00:04:38,030 Let's say I wished I could deconstruct an object. 49 00:04:46,310 --> 00:04:52,430 Unfortunately, this doesn't work because Town does not have the deconstruct method implemented. 50 00:04:52,700 --> 00:04:56,710 Let's fix it by defining the deconstruct extension method. 51 00:05:15,500 --> 00:05:19,460 And now the deconstruction works as expected. 52 00:05:20,030 --> 00:05:20,690 All right. 53 00:05:20,960 --> 00:05:26,900 So we learned that the construction is a mechanism that allows breaking a taboo into variables. 54 00:05:27,140 --> 00:05:33,110 We can also define that the construction for our custom crosses during the interview. 55 00:05:33,170 --> 00:05:38,630 You might be asked what is the difference between the destructor and the deconstruct method? 56 00:05:38,990 --> 00:05:41,180 Well, they are completely different things. 57 00:05:41,510 --> 00:05:47,480 The destructor is a method that's called on an object when it is being removed from memory by the garbage 58 00:05:47,480 --> 00:05:48,140 collector. 59 00:05:48,560 --> 00:05:53,570 The deconstruct method allows the object to be deconstructed into single variables. 60 00:05:53,990 --> 00:06:00,500 It is by default generated for Topos value taboos and positional records, but we can also define it 61 00:06:00,500 --> 00:06:01,700 in custom types. 62 00:06:02,420 --> 00:06:08,060 How can we define the construction, the four types that we did not create, and we don't have access 63 00:06:08,060 --> 00:06:09,140 to the source code. 64 00:06:09,500 --> 00:06:13,760 We can define that deconstruct method as an extension method for this type. 65 00:06:14,360 --> 00:06:16,850 All right, that's it about deconstruction. 66 00:06:17,180 --> 00:06:19,790 Thanks for watching and see you in the next video.