Is this a property, class attribute or what?

2

I have this in my code:

public ICommand ChangePositionCommand => new Command((obj) =>
        {
            _positionIndex++;
            if (_positionIndex >= Positions.Count)
                _positionIndex = 0;
            Position = Positions[_positionIndex];
            RaisePropertyChanged(nameof(Position));
        });

I can say that this is a class attribute, a property or what?

    
asked by anonymous 28.12.2017 / 15:49

2 answers

6

The original answer is down and wrong (no one else noticed - curious how right things get negative and wrong things do not). The right one is:

This is a property. It is quite obvious, it has no parentheses, and every method has its identifier followed by parentheses. How could this be a method?

This is a property that is being initialized with a value, in this case a lambda and you can only get this value since neither a getter nor the getter a setter . In this case the compiler creates the getter to access this value, otherwise it would be better to delete everything since it would be inaccessible.

See how really looks like this in SharpLab .

It does not stop transforming into a get method, but in your code it's a property.

I still think it could be done in a more simplified way, but should be required by some framework .

This is a method. The use of notation => in this case is not appropriate since there is more than one line effectively, although it is a single statement .

I have doubts about whether the code is necessary or should be this way, but it can not be stated without context.

More readable:

public ICommand ChangePositionCommand {
    return new Command((obj) => {
        _positionIndex++;
        if (_positionIndex >= Positions.Count)
            _positionIndex = 0;
        Position = Positions[_positionIndex];
        RaisePropertyChanged(nameof(Position));
    });
}

I placed GitHub for future reference.

Attribute I guarantee it is not .

    
28.12.2017 / 15:56
0

This is a GET method introduced in the latest C # releases:

public int X { get {return A + B;}}

Equivalent to this:

public int X => A + B;
    
29.12.2017 / 13:39