0 1 00:00:06,390 --> 00:00:06,870 Hello. 1 2 00:00:07,110 --> 00:00:14,250 In this video, we're going to talk about question 5: "What are expression-bodied members?" 2 3 00:00:14,250 --> 00:00:15,900 Expression-bodied members of a type 3 4 00:00:16,020 --> 00:00:21,450 are members define which expression body instead of regular body with braces. 4 5 00:00:21,990 --> 00:00:28,380 These sounds are very cryptic, but in practice, it's quite simple, and it allows us to write very concise 5 6 00:00:28,380 --> 00:00:29,550 and readable code. 6 7 00:00:29,790 --> 00:00:32,490 Let's see expression-bodied members in practice. 7 8 00:00:33,090 --> 00:00:34,740 Here is the Person class. 8 9 00:00:35,130 --> 00:00:37,410 Let's focus on the ToString method. 9 10 00:00:37,740 --> 00:00:42,000 This is a regular method and its body is contained in braces. 10 11 00:00:42,480 --> 00:00:45,780 Simple as it is, it takes quite a lot of space. 11 12 00:00:45,900 --> 00:00:50,880 While actually only this part is really defining the logic of this method. 12 13 00:00:51,390 --> 00:00:53,970 This part contains a single expression. 13 14 00:00:54,330 --> 00:00:56,580 But first, what is an expression? 14 15 00:00:57,090 --> 00:01:01,230 And the expression is a piece of code that evaluates to some value. 15 16 00:01:01,740 --> 00:01:10,290 This is an expression that for the Person with Name John and YearOfBirth 1972 will evaluate to string 16 17 00:01:10,380 --> 00:01:13,200 "John who was born in 1972". 17 18 00:01:13,680 --> 00:01:19,740 If a method contains only a single expression, it can be defined as an expression-bodied method. 18 19 00:01:20,190 --> 00:01:22,440 Let's try this with the ToString method. 19 20 00:01:28,120 --> 00:01:29,380 Let's see what happened. 20 21 00:01:29,650 --> 00:01:35,530 First of all, I removed the braces that typically surround the body of the method. 21 22 00:01:35,920 --> 00:01:42,220 I also removed the "return" keyword and simply placed what this method returns to the right side of 22 23 00:01:42,220 --> 00:01:43,420 the arrow operator. 23 24 00:01:43,810 --> 00:01:44,800 And that's it. 24 25 00:01:45,280 --> 00:01:51,250 This method does exactly the same thing as before, and it only takes a single line of code. 25 26 00:01:51,550 --> 00:01:55,640 So the general blueprint for expression-bodied methods is this. 26 27 00:01:56,020 --> 00:02:02,830 We define the return type and the name of the method on the left side of the arrow. On the right side 27 28 00:02:02,860 --> 00:02:06,070 we define an expression whose value will be returned. 28 29 00:02:06,640 --> 00:02:09,640 Expression-bodied methods are short and readable. 29 30 00:02:10,100 --> 00:02:14,050 Their limitation is that they must only contain a single expression. 30 31 00:02:14,470 --> 00:02:19,420 So, for example, this method could not be changed to an expression-bodied method. 31 32 00:02:20,380 --> 00:02:25,540 This method contains more than one expression, as well as several statements. 32 33 00:02:25,960 --> 00:02:31,600 A statement is a piece of code that does something but does not evaluate to a value. 33 34 00:02:31,960 --> 00:02:35,620 For example, Console.Writeline calls are statements. 34 35 00:02:36,310 --> 00:02:42,520 If a method contains a single statement, it can also be changed to a void expression-bodied method. 35 36 00:02:43,060 --> 00:02:44,710 Let's see this in practice. 36 37 00:02:51,530 --> 00:02:56,270 This method simply prints the information about the person to the console. 37 38 00:02:56,630 --> 00:03:00,350 It doesn't return anything as its return type is void. 38 39 00:03:00,980 --> 00:03:01,700 All right. 39 40 00:03:01,820 --> 00:03:07,250 We learned about the expression-bodied methods, but there are more expression-bodied members we 40 41 00:03:07,250 --> 00:03:07,800 can have. 41 42 00:03:08,420 --> 00:03:12,770 One of the most common use cases for them is a read-only property. 42 43 00:03:13,280 --> 00:03:20,390 Let's define an Age property, which will return the current year minus the year of birth of the person. 43 44 00:03:29,800 --> 00:03:34,600 Again, this is extremely simple code, but it takes seven lines. 44 45 00:03:35,020 --> 00:03:38,740 Let's change it to an expression-bodied read-only property. 45 46 00:03:42,970 --> 00:03:43,560 Great. 46 47 00:03:43,780 --> 00:03:46,690 No, it only takes a single line of code. 47 48 00:03:47,350 --> 00:03:52,270 The property doesn't need to be read-only to be defined with expression body. 48 49 00:03:52,750 --> 00:03:56,200 Let's add the LastName property to the Person class. 49 50 00:03:56,620 --> 00:04:00,700 Last names can be changed, most often when someone gets married. 50 51 00:04:00,940 --> 00:04:03,910 So let's enable the modification of this property. 51 52 00:04:15,770 --> 00:04:21,950 This is how the properties looked like before C# 3. For your information in this course 52 53 00:04:22,130 --> 00:04:29,450 we are using C# 10, so you can guess C# 3 was quite some time ago. Starting with C# 4 53 54 00:04:29,450 --> 00:04:35,840 the auto-implemented properties were introduced, allowing us to write code like this. 54 55 00:04:39,140 --> 00:04:41,120 This was a huge improvement. 55 56 00:04:41,270 --> 00:04:45,170 And it still works great in the 99 percent of the cases. 56 57 00:04:45,530 --> 00:04:51,830 But sometimes we want to execute some additional operations during getting and setting of the backing 57 58 00:04:51,830 --> 00:04:52,310 field. 58 59 00:04:52,730 --> 00:04:58,790 For example, we may want to trim the whitespace characters when setting the last name property. 59 60 00:04:59,150 --> 00:05:05,840 In this case, we're not able to use the auto-implemented property because it simply assigns the value 60 61 00:05:05,840 --> 00:05:08,780 on set without any additional operations. 61 62 00:05:09,260 --> 00:05:13,700 So we are stuck with implementing the getter and setter ourselves. 62 63 00:05:14,030 --> 00:05:14,960 Let's do it now. 63 64 00:05:17,750 --> 00:05:21,440 This will work, but it still takes a tremendous amount of space. 64 65 00:05:22,010 --> 00:05:25,550 Let's change this property to an expression-bodied property. 65 66 00:05:32,010 --> 00:05:39,390 And here it is, only 5 lines instead 11. Another thing I wanted to talk about is expression-bodied 66 67 00:05:39,390 --> 00:05:40,650 constructor. 67 68 00:05:41,010 --> 00:05:47,310 The problem with it is that it allows only a single operation to be executed, which here is not the 68 69 00:05:47,310 --> 00:05:47,880 case. 69 70 00:05:48,150 --> 00:05:54,450 We assign both name and year of birth in the constructor, but for the sake of the example, let me 70 71 00:05:54,450 --> 00:05:57,960 define a second constructor which only assigns the name. 71 72 00:06:03,660 --> 00:06:10,230 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 73 00:06:10,230 --> 00:06:12,480 a single operation in the constructor. 73 74 00:06:12,960 --> 00:06:15,710 We can also define expression-bodied destructive. 74 75 00:06:16,530 --> 00:06:18,780 Let's add a destructor to this class. 75 76 00:06:25,800 --> 00:06:31,950 The last members that can be defined with expression body are indexer, but we'll talk more about 76 77 00:06:31,950 --> 00:06:33,870 them later in the course. 77 78 00:06:34,350 --> 00:06:37,660 Let's summarize. Expression-bodiedmembers of a type 78 79 00:06:37,680 --> 00:06:44,790 are members defined with expression body instead of the regular body with braces. To define them, we 79 80 00:06:44,790 --> 00:06:50,640 must use the arrow operator. Expression-bodied members allow us to make the code much shorter. 80 81 00:06:51,210 --> 00:06:56,880 During the interview, you may hear questions like "What is an expression?" 81 82 00:06:56,880 --> 00:07:00,690 Expression is a piece of code that evaluates to some value. 82 83 00:07:00,930 --> 00:07:04,680 For example, two plus five evaluates to seven. 83 84 00:07:05,400 --> 00:07:06,930 "What is a statement?" 84 85 00:07:07,620 --> 00:07:13,410 A statement is a piece of code that does something, but it doesn't evaluate one value. 85 86 00:07:13,620 --> 00:07:17,700 For example, calling the Console.Writeline method is a statement. 86 87 00:07:18,030 --> 00:07:23,070 It does not evaluate any value as the Console.Writeline method is void. 87 88 00:07:23,730 --> 00:07:29,430 All right, that's it about the expression-bodied members. Thanks for watching, and I'll see you 88 89 00:07:29,430 --> 00:07:30,480 in the next lecture.