This week, we’re discussing Expression-Bodied Members in C# 7.0. Now, this was an existing feature in C# 6.0, but it was limited to only methods, like so:
1 |
public void DoAThingCS6() => Console.WriteLine("This is a test"); |
With the new additions in C# 7.0, we can now do Constructors, Deconstructors, and Properties. A Constructor definition looks like this:
1 |
public SomeClass() => DoAThing(); |
Similarly, a Deconstructor looks like this:
1 |
~SomeClass() => DoAThing(); |
Finally, although auto-properties are shorter, you can write properties with specific implementations with this new shorthand:
1 2 3 4 5 6 |
private string someField; public string SomeProperty { get => someField; set => someField = value; } |
That’s it for this week. Next week, we’ll go over Pattern Matching.