0 1 00:00:07,230 --> 00:00:07,740 Hello. 1 2 00:00:08,250 --> 00:00:15,300 The first interview question we'll talk about is "What is the difference between tuples and ValueTuples?" 2 3 00:00:15,600 --> 00:00:22,830 First, let's make sure we understand what tuples are on the conceptual level. Tuples are small data 3 4 00:00:22,830 --> 00:00:27,120 structures used to bundle a couple of pieces of information together. 4 5 00:00:27,750 --> 00:00:34,170 This can be useful when, for example, I wanted to create a method that needs to return two pieces of 5 6 00:00:34,170 --> 00:00:38,430 information without declaring a dedicated type for it. 6 7 00:00:39,090 --> 00:00:42,960 Let's create a simple tuple holding an int and a string. 7 8 00:00:48,440 --> 00:00:55,460 I could also create a tuple using the Tuple.Create method, which is more convenient as it doesn't require 8 9 00:00:55,460 --> 00:01:00,590 providing the type parameters. It confers them from provided values. 9 10 00:01:04,590 --> 00:01:10,980 I can access the values held in the tuple using the Item1 and Item2 properties. 10 11 00:01:15,800 --> 00:01:19,680 Please be aware that we can hold more than two elements in a tuple. 11 12 00:01:20,090 --> 00:01:21,590 The maximum is eight. 12 13 00:01:21,890 --> 00:01:27,770 So calling the Create method or the constructor with more than eight parameters will not compile. 13 14 00:01:32,250 --> 00:01:39,060 There is a way of bypassing this limitation by simply making the tuple nested. We can just declare 14 15 00:01:39,180 --> 00:01:40,530 a tuple of tuples. 15 16 00:01:48,880 --> 00:01:51,700 In C@ we have two kinds of tuples: 16 17 00:01:52,000 --> 00:01:54,370 Regular tuples and ValueTuples. 17 18 00:01:54,760 --> 00:01:57,760 Before we move on, let me clarify one thing. 18 19 00:01:58,030 --> 00:02:04,990 From now on, when I will be using the word "tuple", I will be meaning the System.Tuple type. When I will have 19 20 00:02:04,990 --> 00:02:06,580 ValueTuple in mind 20 21 00:02:06,760 --> 00:02:08,440 I will use its full name. 21 22 00:02:09,280 --> 00:02:09,910 All right. 22 23 00:02:10,180 --> 00:02:13,780 Let's see some ValueTuples. On the conceptual level 23 24 00:02:14,020 --> 00:02:19,210 they serve the same purpose as tuples, so they bundle a couple of values together. 24 25 00:02:19,570 --> 00:02:21,580 I could create a ValueTuple 25 26 00:02:21,580 --> 00:02:22,630 with a constructor call. 26 27 00:02:29,880 --> 00:02:34,080 But for ValueTuples, there is a much simpler way to create them. 27 28 00:02:38,710 --> 00:02:44,620 As you can see, the construction of a ValueTuple looks much nicer than the creation of a tuple. 28 29 00:02:45,160 --> 00:02:49,270 Let's discuss other differences between tuples and ValueTuples. 29 30 00:02:49,780 --> 00:02:55,030 First of all, tuple is a reference type, while ValueTuple is a value type. 30 31 00:02:55,630 --> 00:02:57,670 This has a lot of implications. 31 32 00:02:57,820 --> 00:03:04,210 For example, tuples are compared by reference while ValueTuples are compared by value. 32 33 00:03:04,600 --> 00:03:11,010 We can compare tuples by value if we need to, buy using the Equals method, which tuple overrides. 33 34 00:03:11,400 --> 00:03:15,100 Let's see some code that compares tuples and ValueTuples. 34 35 00:03:15,850 --> 00:03:21,460 All right, those two tuples and those two ValueTuples are equal by value. 35 36 00:03:21,820 --> 00:03:24,400 Let's see what will be presented to the console. 36 37 00:03:25,780 --> 00:03:32,670 As you can see, the euqlity operator gives false for tuples because as reference types, they are compared 37 38 00:03:32,710 --> 00:03:40,450 by reference. The fact that tuples are reference types can have negative performance implications. 38 39 00:03:40,750 --> 00:03:47,230 Couples are usually short-lived objects, and if we create a lot of them, the process of allocating 39 40 00:03:47,230 --> 00:03:50,710 and freeing the memory might take considerable time. 40 41 00:03:51,070 --> 00:03:54,820 This was one of the reasons why ValueTuples were introduced 41 42 00:03:54,880 --> 00:04:02,050 with C# 7. The next difference that we must be aware of is that tuples are immutable and ValueTuples 42 43 00:04:02,050 --> 00:04:03,370 are mutable. 43 44 00:04:03,790 --> 00:04:09,550 If an object is immutable, it means it cannot be modified once it has been created. 44 45 00:04:09,910 --> 00:04:14,080 We'll learn more about immutable types later in the course. 45 46 00:04:19,130 --> 00:04:25,220 As you can see, modifying the item of a tuple doesn't work, but it works fine for ValueTuples. 46 47 00:04:25,820 --> 00:04:32,360 The difference that probably matters most for us as the developers, is that for ValueTuples we can 47 48 00:04:32,360 --> 00:04:35,120 give names to the fields that they contain 48 49 00:04:35,240 --> 00:04:39,800 and we don't need to use those awkward Item1 and Item2 nams. 49 50 00:04:40,160 --> 00:04:42,740 However, we still can if we want to. 50 51 00:04:59,260 --> 00:05:02,440 As you can see, this looks much better than this. 51 52 00:05:03,010 --> 00:05:08,170 The last difference is that we can create ValueTuples with more than eight elements. 52 53 00:05:16,940 --> 00:05:22,640 In the document attached to this lecture, you can read in detail how it works under the hood. 53 54 00:05:23,090 --> 00:05:29,690 All right, let's summarize the differences between tuples and the ValueTuples. Tuples are reference 54 55 00:05:29,690 --> 00:05:35,990 types, ValueTuples are value types. When a lot of short-lived tuples are created 55 56 00:05:36,230 --> 00:05:42,230 it may decrease the performance of the application as the memory management for reference types is more 56 57 00:05:42,230 --> 00:05:44,150 demanding than for value types. 57 58 00:05:44,650 --> 00:05:49,700 ValueTuples provide a convenient syntax for creation. In tuples 58 59 00:05:49,880 --> 00:05:53,990 all properties are named Item1, Item2, and so on. 59 60 00:05:54,590 --> 00:05:57,630 The fields of the ValueTuples can have names. 60 61 00:05:58,250 --> 00:05:59,840 Tuples are immutable. 61 62 00:06:00,230 --> 00:06:02,030 ValueTuples are mutable. 62 63 00:06:02,630 --> 00:06:07,070 ValueTuples can easily be declared with more than eight elements. 63 64 00:06:07,940 --> 00:06:14,060 When discussing tuples during the interview, you can be asked the following question: "Is it possible 64 65 00:06:14,060 --> 00:06:16,670 to have a tuple with more than eight elements?" 65 66 00:06:17,270 --> 00:06:23,930 Well, tuples are limited to hold up to eight elements, and the only way to bypass this limitation 66 67 00:06:24,140 --> 00:06:25,820 is to make the tuple nested. 67 68 00:06:26,060 --> 00:06:32,620 So, for example, we can have a yuple of eight elements in which the last one is also a yuple of eight 68 69 00:06:32,630 --> 00:06:34,910 elements. For ValueTuples 69 70 00:06:35,030 --> 00:06:38,480 we can easily declare them with more than eight elements. 70 71 00:06:39,230 --> 00:06:41,480 All right, that's it for this lecture. 71 72 00:06:42,050 --> 00:06:44,660 Thanks for watching, and I'll see you in the next one.