50

What does this code mean?

public bool property => method();
Daniel Cassidy
  • 24,676
  • 5
  • 41
  • 54
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • Maybe you should search for lambda operator – meJustAndrew Oct 27 '16 at 10:38
  • 4
    @meJustAndrew that's not a lambda operator. It's an expression bodied member, a *new* language feature – Panagiotis Kanavos Oct 27 '16 at 10:39
  • 3
    Search for "expression bodied properties". – Matt Hogan-Jones Oct 27 '16 at 10:39
  • @PanagiotisKanavos [Actually is is](https://msdn.microsoft.com/en-us/library/bb311046.aspx) – DavidG Oct 27 '16 at 10:40
  • @PanagiotisKanavos you are right, is to specify the return of an only get property, C#6 stuff – meJustAndrew Oct 27 '16 at 10:40
  • @DavidG the OP is asking about expression bodied properties, not lambdas. Looking for lambdas is *not* going to return helpful results. You can't use the lambda operator this way in C#5 – Panagiotis Kanavos Oct 27 '16 at 10:42
  • 1
    @Liam definitely not. The OP is asking about expression-bodied properties, not lambdas. – Panagiotis Kanavos Oct 27 '16 at 10:44
  • You should take a look at the new language features: https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6 – QBrute Oct 27 '16 at 10:59
  • @PanagiotisKanavos You’ve commented on two (now-deleted) answers telling them not to link to duplicates. Please be aware that these answers didn’t link to duplicate questions but (completely correctly) to the relevant documentation. – Konrad Rudolph Oct 27 '16 at 12:29
  • @KonradRudolph the answerer himself in one of those questions pointed out that link-only questions aren't good even when they point to SO documentation. The other answer was wrong, confusing expression-bodied members for lambdas – Panagiotis Kanavos Oct 27 '16 at 12:35
  • @PanagiotisKanavos Yes, but link-only is unrelated to the fact that links to documentation are not the same as links to duplicate questions. I’m not contesting that these answers were deleted, I just wanted to make you aware of Stack Overflow’s documentation section. Regarding the correctness of the second answer, the Roslyn announcement itself calls expression-bodied functions “lambda expressions” which I find confusing but well. – Konrad Rudolph Oct 27 '16 at 12:41
  • Asking and answering such question once a year seems to bring a lot of attention. – Sinatr Oct 27 '16 at 12:43

6 Answers6

65

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;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 7
    The correct (ie googlable) name is "expression-bodied property" – Panagiotis Kanavos Oct 27 '16 at 10:40
  • The only explanation in the answers. – mybirthname Oct 27 '16 at 10:44
  • 4
    First time I've seen, this my first reaction to it is, *why?*. At no point have I ever looked at the property and gone, *you know what that needs, an arbitrary lambda type syntax*... Maybe a slow day at Redmond – Liam Oct 27 '16 at 10:56
  • The [MSDN Blog](https://msdn.microsoft.com/en-us/magazine/dn802602.aspx) seems to agree *There’s nothing particularly radical about expression bodied functions* – Liam Oct 27 '16 at 11:00
  • 2
    @Liam I have been using this feature for a few months now. It is particularly good for wrapper classes with lots of delegation, but it occasionally helps with a property or two in a regular class. – Sergey Kalinichenko Oct 27 '16 at 11:03
  • Got my vote for teaching me it can be used in methods. – lozzajp Oct 27 '16 at 11:41
  • @Liam When you have computable properties, for example a `Vector` class, where most operations and properties are a simple mathematical formula, which fit in one line. For example, you could have a `public Vector Normalized => this / this.Length;` property, which looks much better than all the nested braces. – This company is turning evil. Oct 27 '16 at 12:37
  • 1
    @Liam IMHO C# has a whitespace problem, i.e. way too many lines with, say, one curly brace. Making some constructs more compact improves readability, at least for me. – Kirill Rakhman Oct 27 '16 at 15:14
19

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?
mshwf
  • 7,009
  • 12
  • 59
  • 133
11

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);
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
stop-cran
  • 4,229
  • 2
  • 30
  • 47
5

=> 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.

Tinwor
  • 7,765
  • 6
  • 35
  • 56
3

It's the expression bodied simplification.

public string Text =>
  $"{TimeStamp}: {Process} - {Config} ({User})";

Reference; https://msdn.microsoft.com/en-us/magazine/dn802602.aspx

Community
  • 1
  • 1
Nadeem Khoury
  • 886
  • 6
  • 18
-3

That is a expression bodied property. It can be used as a simplification from property getters or method declarations. From C# 7 it was also expanded to other member types like constructors, finalizers, property setters and indexers.

Check the MSDN documentation for more info.

"Expression body definitions let you provide a member's implementation in a very concise, readable form. You can use an expression body definition whenever the logic for any supported member, such as a method or property, consists of a single expression."

Sérgio Azevedo
  • 313
  • 1
  • 4
  • 13
  • 5
    No it is not. It is an expression-bodied member – Stilgar Oct 27 '16 at 10:42
  • 1
    This answer shouldn't be downvoted as a *lot* of people don't know about expression bodied members. Including some of the downvoters I'd guess. That doesn't make this a bad answer. In C# 5 this would be a *correct* answer. It should be deleted though to remove the negative rep – Panagiotis Kanavos Oct 27 '16 at 12:39