Search code examples
c#.netoverridingsubclassnotation

Use of notation => in a subclass


What does the following mean when written in a subclass:

public override bool IsReadOnly
    => false;

Does this differ from:

public override bool IsReadonly
{
  get 
  {
    return false;
  }
}

Solution

  • What does the following mean when written in a subclass

    This is a new feature in C# 6.0 called Expression Body, this is a syntactic sugar that allows define getter-only properties and indexers where the body of the getter is given by the expression body.

    Does this differ from

    No, there is no difference between this and the classic form, is just an syntactic sugar.

    Methods as well can be defined as an expression-bodied:

    public void PrintLine(string line) => Console.WriteLine(line);