Tor Klingberg
Tor Klingberg

Reputation: 5088

Compiler error for lambda expression

public ICommand ChangeLangCommand => new DelegateCommand(this.ChangeLangClick);

I get this compiler error, marked on the => arrow:

Error   1   ; expected

Am I using the wrong compiler version somehow? How can I check this?

Upvotes: 1

Views: 1833

Answers (1)

Sinatr
Sinatr

Reputation: 21999

This is a C# 6.0 feature called expression bodied property

public ICommand ChangeLangCommand => new DelegateCommand(this.ChangeLangClick);

You can either upgrade your compiler (install latest release version of VS2015) or don't use it, as it's equal to getter-only property:

public ICommand ChangeLangCommand
{
    get
    {
        return new DelegateCommand(this.ChangeLangClick);
    }
}

I have feeling, what creating new instance of the command every time property is accessed is wrong, correct code may/would be

public ICommand ChangeLangCommand { get; }

// in constructor
ChangeLangCommand = new DelegateCommand(this.ChangeLangClick);

I think this is also a new feature (to initialize getter-only property from constructor), if this is true, then you can use older syntax:

ICommand _changeLangCommand;
public ICommand ChangeLangCommand
{
    get
    {
        return _changeLangCommand;
    }
}

// in constructor
_changeLangCommand = new DelegateCommand(this.ChangeLangClick);

Upvotes: 1

Related Questions