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