The last but not the least feature of the C# 6.0 that I am going to cover is expression-bodied methods.
We all have experience writing single line methods:
private string name;
public override string ToString()
{
return "Name :" + name;
}
Now, we have shorter way of defining the same method:
public override string ToString() => "Name: " + name;
As you can see, now we can use lambda style to define method's body.
But there is one difference from lambda expressions, we cannot use curly brackets to define multiline method's body.
Internal Implementation
This syntax has nothing to do with lambda expressions (it is just look like lambda expression), there are not closures, etc.
It is just a syntactic sugar for old-style method definition.
As you can see in IL code, this syntax will compile to the simple method, the same as old-style method definition:
.method public hidebysig virtual instance string
ToString() cil managed
{
// Code size 22 (0x16)
.maxstack 2
.locals init ([0] string V_0)
IL_0000: nop
IL_0001: ldstr "Name :"
IL_0006: ldarg.0
IL_0007: ldfld string ConsoleApplication2.Program::name
IL_000c: call string [mscorlib]System.String::Concat(string,
string)
IL_0011: stloc.0
IL_0012: br.s IL_0014
IL_0014: ldloc.0
IL_0015: ret
} // end of method Program::ToString
So, you can safely use new syntax without worrying about performance.