14

What's the difference between these two ways to add something?

private string abc => "def";

And

private string abc = "def";
Jcl
  • 27,696
  • 5
  • 61
  • 92
kb_
  • 620
  • 4
  • 21

1 Answers1

25

This is the declaration of a classic field as it has always existed in C#:

private string abc = "def";

The field is immediately assigned an initial value.


This is a shorthand syntax for declaring a getter-only property (or expression-bodied property), introduced in C# 6:

private string abc => "def";

It's a short way to write the following:

private string abc
{
    get { return "def"; }
}
Marius Schulz
  • 15,976
  • 12
  • 63
  • 97