-2

I'm looking at Protocol Buffers for C#, and there's code like this:

public sealed class FileDescriptor : IDescriptor
{
    // snip

    /// <value>
    /// The file name.
    /// </value>
    public string Name => Proto.Name;

    /// <summary>
    /// The package as declared in the .proto file. This may or may not
    /// be equivalent to the .NET namespace of the generated classes.
    /// </summary>
    public string Package => Proto.Package;

    // etc.

=> seems to be the lambda operator, but this doesn't look much like a lambda. What's going on here?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • it's a duplicate, it was asked couple of days back as well, but its old duplicate – Ehsan Sajjad Feb 19 '16 at 18:47
  • That's c# 6.0, it's like creating this: public string Name{ get{ return Proto.Name; } set{ Proto.Name = value; }} – Gusman Feb 19 '16 at 18:48
  • 1
    Voting is so weird on this site. Yes, it's a duplicate, but is it such a stupid question, when "=>" is impossible to search for on Stack Overflow (it just ignores it), and Microsoft's own documentation doesn't even mention this? – Brendan Long Feb 19 '16 at 18:58

1 Answers1

2

This is C#6 expression-bodied members. They let you specify the property implementation using lambda syntax.

This:

public string Name => Proto.Name;

Is equivalent to:

public string Name { get { return Proto.Name; } }
driis
  • 161,458
  • 45
  • 265
  • 341
  • bodied properties did not implement also a setter? Just curious, i'm still learning 6.0 – Gusman Feb 19 '16 at 18:58
  • @Gusman They do not implement a setter, it wouldn't make sense. For example, you could write `public string Name => "My Name";` or `public string Name => Proto.Name + " More Name";` In that case what could it actually set? – Kyle Feb 19 '16 at 19:50
  • Nice catch, thanks a lot for the info – Gusman Feb 19 '16 at 19:55