Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "=>" operator mean in a property in C#? [duplicate]

Tags:

c#

c#-6.0

What does this code mean?

public bool property => method();
like image 494
Michael Haddad Avatar asked Oct 27 '16 10:10

Michael Haddad


People also ask

What is the => operator in C#?

=> used in property is an expression body . Basically a shorter and cleaner way to write a property with only getter . public bool MyProperty => myMethod(); It's much more simpler and readable but you can only use this operator from C# 6 and here you will find specific documentation about expression body.

What does => mean in Linq?

The => operator can be used in two ways in C#: As the lambda operator in a lambda expression, it separates the input variables from the lambda body. In an expression body definition, it separates a member name from the member implementation.

What are accessors in C#?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

What is lambda operator in C#?

The lambda operator => divides a lambda expression into two parts. The left side is the input parameter and the right side is the lambda body.


4 Answers

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. This syntax is equivalent to

public bool property {
    get {
        return method();
    }
}

Similar syntax works for methods, too:

public int TwoTimes(int number) => 2 * number;
like image 110
Sergey Kalinichenko Avatar answered Oct 18 '22 20:10

Sergey Kalinichenko


As some mentioned this is a new feature brought first to C# 6, they extended its usage in C# 7.0 to use it with getters and setters, you can also use the expression bodied syntax with methods like this:

static bool TheUgly(int a, int b)
{
    if (a > b)
        return true;
    else
        return false;
}
static bool TheNormal(int a, int b)
{
    return a > b;
}
static bool TheShort(int a, int b) => a > b; //beautiful, isn't it?
like image 37
mshwf Avatar answered Oct 18 '22 19:10

mshwf


That's an expression bodied property. See MSDN for example. This is just a shorthand for

public bool property
{
    get
    {
        return method();
    }
}

Expression bodied functions are also possible:

public override string ToString() => string.Format("{0}, {1}", First, Second);
like image 11
stop-cran Avatar answered Oct 18 '22 20:10

stop-cran


=> used in property is an expression body. Basically a shorter and cleaner way to write a property with only getter.

public bool MyProperty {
     get{
         return myMethod();
     }
}

Is translated to

public bool MyProperty => myMethod();

It's much more simpler and readable but you can only use this operator from C# 6 and here you will find specific documentation about expression body.

like image 5
Tinwor Avatar answered Oct 18 '22 21:10

Tinwor