32

When reading a project, I found some strange C# code:

public class F : IElement
{
    public int CurrentHp { get; } = 10;
    public bool IsDead => CurrentHp <= 0;
}

Normally I would write something like:

public class F : IElement
{
    public const int CurrentHp = 10;
    public bool IsDead
    {
        get { return CurrentHp <= 0; }
    }
}

My Visual Studio 2013 also cannot recognize the first example.

What is this syntax and what should I do to make this project buildable?

Kirill
  • 7,580
  • 6
  • 44
  • 95
kuang
  • 233
  • 3
  • 8
  • 6
    I wouldn't use const for a dynamic health value. – ave Feb 24 '16 at 15:27
  • 5
    `=> CurrentHp <=` is certainly an unfortunate artefact of the lambda/expression-bodied-function syntax. I would at least write `IsDead => (CurrentHp <= 0)`. – Tavian Barnes Feb 24 '16 at 16:36

4 Answers4

34

=> is a new operator in C# 6 and indicates an Expression Bodied Function to use for that getter.

Your two examples are synonymous as far as the compiler is concerned and merely return the value assigned. The => is syntactic sugar to make development a bit easier and require fewer lines of code to achieve the same outcome.

However, you won't be able to compile unless you update to VS2015 with the latest compiler version.

Edit:

As said by Philip Kendall and Carl Leth in the comments, the first lines in each are not exactly synonymous as public const int CurrentHp = 10; is a field and public int CurrentHp { get; } = 10; is a property. Although at a high level the outcome is the same (assigning a value of 10 to CurrentHp with the property only being settable in the class constructor), they differ in that:

With const int CurrentHp = 10, CurrentHp will always be 10, take up 4 total bytes, and can be accessed statically. int CurrentHp { get; } = 10defaults to 10, but can be changed in the constructor of F and thereby can be different per instance and cannot be accessed statically.

Steve
  • 9,335
  • 10
  • 49
  • 81
  • 4
    I don't think the two examples are equivalent as `CurrentHp` is a property in the C# 6 version but a field in the older version. – Philip Kendall Feb 24 '16 at 08:50
  • 1
    As an example of the difference @PhilipKendall noted, with `const int CurrentHp = 10`, CurrentHp will always be 10, take up 4 total bytes, and can be accessed statically. `int CurrentHp { get; } = 10` defaults to 10, but can be changed in the constructor of `F` and thereby can be different per instance and cannot be accessed statically. – JounceCracklePop Feb 24 '16 at 08:53
  • 1
    In the second example CurrentHp will not only always be 10, even other assemblies that use it, will treat it as a compile time constant and therefore never even ask your class for this value, ever. Be very careful with public const fields. – linac Feb 24 '16 at 14:37
  • You can use VS2013 with C# 6. Try this out: [Install-Package Microsoft.Net.Compilers](http://www.nuget.org/packages/Microsoft.Net.Compilers/) :) – Joe Feb 24 '16 at 16:55
  • @Steve Of course it's ok! I still have a minor disagreement with your wording, though: you state that in both cases, the value can only be retrieved but not set at runtime, but that's not quite right with the auto-property. The value *can* be set at runtime, but only in the class's constructor. 10 is just the default value if the constructor doesn't set it. (In fact that's the whole reason for having it -- to make truly immutable types much easier to implement). See [this .NetFiddle example](https://dotnetfiddle.net/J3hp6S) – JounceCracklePop Feb 24 '16 at 22:20
23

It's C# 6 features: New Language Features in C# 6.

The first

public int CurrentHp { get; } = 10;

is Getter-only auto-propertiey.

The second

public bool IsDead => CurrentHp <= 0;

is Expression bodies on property-like function members.

Kirill
  • 7,580
  • 6
  • 44
  • 95
7

As other people said it's new C# 6 features. Check full list at 1

However more correctly this would translate to this in before C# 6:

public class F : IElement
{
    public int CurrentHp { get; private set };
    public bool IsDead { get { return CurrentHp <= 0; } }
    public F() { CurrentHp = 10; }
}

New Language Features in C# 6

aiodintsov
  • 2,545
  • 15
  • 17
1

This is your code if you use C# 6:

public bool IsDead => CurrentHp <= 0;

It is just a property with a get, if use this operator =>.

In earlier versions you would write something like this:

public bool IsDead { get { return CurrentHp <= 0; } }
Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
daniel59
  • 906
  • 2
  • 14
  • 31