moien moien - 1 month ago 20
C# Question

C# 6.0 => Operator

I'm using visual studio 2013. I found a project in github, that project is using Visual Studio 2015. I'm trying to compile it but I get an error using vs2013


Invalid token ';' in class, struct, or interface member declaration


Here code's :

private readonly Log _log;
public Log Log => _log; // What => Operator is doing ? Pointer ?


Yes, What new features of
=>
operator in
C# [6.0]
? and is there any way to use C# 6.0 in vs2013?

Answer

This is an expression-bodied property, a new syntax for computed properties introduced in C# 6, which lets you create computed properties in the same way as you would create a lambda expression. So:

public int TwoTimes(int number)
{
    return 2*number;
}

is equivalent to

public int TwoTimes(int number) => 2 * number;

Note: C# 6.0 was introduced with VS 2015. You can not use it with an earlier version.

Ref: What does "=>" operator mean in a property in C#?