Home > Software engineering >  Inconsistent body style, use expression body
Inconsistent body style, use expression body

Time:10-05

I have this code:

private string _class;
public string Class
{
    get
    {
        return string.IsNullOrEmpty(_class) ? "preview" : _class;
    }
    set
    {
        _class = value;
    }
}

How can this be simplified using expression body?

CodePudding user response:

Expression bodies are a new feature since C#7. If your getter or setter has only one line, you can simplify them with a syntax similar to lambda expressions to

public string Class
{
    get => string.IsNullOrEmpty(_class) ? "preview" : _class;
    set =>  _class = value;
}
  • Related