Hello. 1
In this video, we're going to talk about question 5: "What are expression-bodied members?" 2
Expression-bodied members of a type 3
are members define which expression body instead of regular body with braces. 4
These sounds are very cryptic, but in practice, it's quite simple, and it allows us to write very concise 5
and readable code. 6
Let's see expression-bodied members in practice. 7
Here is the Person class. 8
Let's focus on the ToString method. 9
This is a regular method and its body is contained in braces. 10
Simple as it is, it takes quite a lot of space. 11
While actually only this part is really defining the logic of this method. 12
This part contains a single expression. 13
But first, what is an expression? 14
And the expression is a piece of code that evaluates to some value. 15
This is an expression that for the Person with Name John and YearOfBirth 1972 will evaluate to string 16
"John who was born in 1972". 17
If a method contains only a single expression, it can be defined as an expression-bodied method. 18
Let's try this with the ToString method. 19
Let's see what happened. 20
First of all, I removed the braces that typically surround the body of the method. 21
I also removed the "return" keyword and simply placed what this method returns to the right side of 22
the arrow operator. 23
And that's it. 24
This method does exactly the same thing as before, and it only takes a single line of code. 25
So the general blueprint for expression-bodied methods is this. 26
We define the return type and the name of the method on the left side of the arrow. On the right side 27
we define an expression whose value will be returned. 28
Expression-bodied methods are short and readable. 29
Their limitation is that they must only contain a single expression. 30
So, for example, this method could not be changed to an expression-bodied method. 31
This method contains more than one expression, as well as several statements. 32
A statement is a piece of code that does something but does not evaluate to a value. 33
For example, Console.Writeline calls are statements. 34
If a method contains a single statement, it can also be changed to a void expression-bodied method. 35
Let's see this in practice. 36
This method simply prints the information about the person to the console. 37
It doesn't return anything as its return type is void. 38
All right. 39
We learned about the expression-bodied methods, but there are more expression-bodied members we 40
can have. 41
One of the most common use cases for them is a read-only property. 42
Let's define an Age property, which will return the current year minus the year of birth of the person. 43
Again, this is extremely simple code, but it takes seven lines. 44
Let's change it to an expression-bodied read-only property. 45
Great. 46
No, it only takes a single line of code. 47
The property doesn't need to be read-only to be defined with expression body. 48
Let's add the LastName property to the Person class. 49
Last names can be changed, most often when someone gets married. 50
So let's enable the modification of this property. 51
This is how the properties looked like before C# 3. For your information in this course 52
we are using C# 10, so you can guess C# 3 was quite some time ago. Starting with C# 4 53
the auto-implemented properties were introduced, allowing us to write code like this. 54
This was a huge improvement. 55
And it still works great in the 99 percent of the cases. 56
But sometimes we want to execute some additional operations during getting and setting of the backing 57
field. 58
For example, we may want to trim the whitespace characters when setting the last name property. 59
In this case, we're not able to use the auto-implemented property because it simply assigns the value 60
on set without any additional operations. 61
So we are stuck with implementing the getter and setter ourselves. 62
Let's do it now. 63
This will work, but it still takes a tremendous amount of space. 64
Let's change this property to an expression-bodied property. 65
And here it is, only 5 lines instead 11. Another thing I wanted to talk about is expression-bodied 66
constructor. 67
The problem with it is that it allows only a single operation to be executed, which here is not the 68
case. 69
We assign both name and year of birth in the constructor, but for the sake of the example, let me 70
define a second constructor which only assigns the name. 71
As you can see, it's also a nice one-liner, so it may be still a good idea to use it if we only execute 72
a single operation in the constructor. 73
We can also define expression-bodied destructive. 74
Let's add a destructor to this class. 75
The last members that can be defined with expression body are indexer, but we'll talk more about 76
them later in the course. 77
Let's summarize. Expression-bodiedmembers of a type 78
are members defined with expression body instead of the regular body with braces. To define them, we 79
must use the arrow operator. Expression-bodied members allow us to make the code much shorter. 80
During the interview, you may hear questions like "What is an expression?" 81
Expression is a piece of code that evaluates to some value. 82
For example, two plus five evaluates to seven. 83
"What is a statement?" 84
A statement is a piece of code that does something, but it doesn't evaluate one value. 85
For example, calling the Console.Writeline method is a statement. 86
It does not evaluate any value as the Console.Writeline method is void. 87
All right, that's it about the expression-bodied members. Thanks for watching, and I'll see you 88
in the next lecture.