1

i just need to know what is the difference between these two lines

private string somestring => "string";
private string somestring = "string";

what is the difference of there uses they just prints same to the console

  • "=>" does not initialize a string. Instead, it specifies "somestring" as a property returns "string". – weichch Apr 06 '20 at 04:19

1 Answers1

6
private string somestring => "string";

This is an expression-bodied property, and is equivalent to the following:

private string somestring { get { return "string"; } }

Whereas the following is just a regular field:

private string somestring = "string";

See this related question on Properties vs Fields for more information.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    oh thanks so that means it is a readonly didn't figured it out thanks again –  Apr 06 '20 at 04:20
  • 1
    @YonalJayasinghe Yes, but note that if you want read-only _fields_, it's also possible to create one of those using the `readonly` keyword. – ProgrammingLlama Apr 06 '20 at 04:27