1

I know the C# operator => is a lambda operator. But today I came across the operator being used this way:

static readonly ResourceDictionary ResourceDictionary = new ResourceDictionary();
public static ResourceDictionary MyAppResources => ResourceDictionary;

Here it doesn't appear to be functioning as a lambda operator. Can anyone tell me what this operator does when used like that?

jbyrd
  • 5,287
  • 7
  • 52
  • 86

1 Answers1

5

=> is not really an operator in this context, in the sense that it is not used in an expression. This is the new C# 6 syntax for defining expression-bodied properties, an equivalent of

public static ResourceDictionary MyAppResources {
    get {
        return ResourceDictionary;
    }
}

in the old syntax. You can write expression-bodied methods as well, for example

public string ToString() => $"User [{FirstName} {LastName}]";

instead of

public string ToString() {
    return string.Format("User [{0} {1}]", FirstName, LastName);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Interesting! Isn't an expression-bodied method be a lambda expression? – jbyrd Mar 22 '16 at 17:28
  • @jbyrd I think all this is is syntactic sugar: the result of compiling expression-bodied methods and properties is always a named method or a getter of a named property, not a lambda. – Sergey Kalinichenko Mar 22 '16 at 17:42