Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Expression Bodied Members2:52 with Jeremy McLain
This syntactic sugar removes the braces from simple methods and properties and makes them one-liners.
Many people consider C# to be a fairly verbose language where 0:00 curly braces abound. 0:04 On the other hand, C# contains a lot of syntactic sugar. 0:05 Syntactic sugar is a feature in a programming language 0:09 that helps with writing more concise code. 0:12 They provide alternative ways to write the same code using fewer keystrokes. 0:15 We recently saw one example of syntactic sugar, the ternary if statement. 0:20 So instead of typing this, we only need to type this. 0:25 Another example is auto-properties, 0:30 where instead of typing this we only need to type this. 0:33 The result of using syntactic sugar in general is shorter more concise code. 0:38 When code gets long and requires lots of scrolling in order to read it all, 0:44 it can get difficult to see how it all works together. 0:48 On the other hand, when code gets condensed, 0:51 it can also become difficult to read. 0:54 This is the balance beam that all programmers need to walk. 0:56 Code should be both clear and concise. 1:00 But above all, it should be clear. 1:03 So when choosing to use syntactic sugar, 1:05 we should always ask ourselves, is this clear and more readable? 1:07 It turns out we can make the code we've written so far for 1:12 the Invader class even more concise. 1:15 Let's take yet another look at the Location property. 1:18 Really, all this property does is call a single method and 1:21 return whatever it returns, nothing more. 1:24 We could make this even more obvious by writing it like so. 1:27 This line is completely equivalent to the previous code. 1:37 Location is still a property. 1:40 The Location property reduces to just calling _path.GetLocationAt. 1:42 So long as this part on the right side evaluates to a MapLocation, and 1:46 you can write it in a single statement, then you can write it like this. 1:50 Of course, this only makes sense with computed properties. 1:54 We can do something similar with methods. 1:59 Let's take a look at the Move method. 2:01 The Move method only has a single line in it. 2:04 We can rewrite it like so. 2:08 This looks very similar to what we just did with the location property. 2:13 Move is still a method though. 2:17 We know that because we still have the parentheses here. 2:19 The other difference between methods and 2:23 properties written this way, is that properties always return a value. 2:25 Our Move method doesn't return a value. 2:29 It's still void. 2:32 It just adds one to the _pathStep field. 2:34 Because the Move method only contained one line, 2:37 we can write it as a single line, like so. 2:40 You don't have to write your code this way. 2:43 This is purely syntactic sugar. 2:45 I'm only showing it to you here because it's likely that you'll see this in other 2:47 code you read. 2:50
You need to sign up for Treehouse in order to download course files.Sign up