Is that a Property, class attribute or what?

Asked

Viewed 116 times

3

I have it in my code:

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

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

  • See the Reply Issue.

2 answers

7


The original answer is below and is wrong because the new way of writing gave room to misunderstand (no one else noticed - curious how right things get negative and wrong things don’t). The certain is:

This is a property. It’s 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 case a lambda and that you can only take this value since it was not created nor a getter neither 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 it’s really like that in the Sharplab.

Doesn’t stop turning into a method get, but in your code is a property.

I still think it could be done in a more simplified way, but it must be some requirement 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 doubt whether the code is necessary or should be so, but it is not possible to say 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 put in the Github for future reference.

Attribute I guarantee it’s not.

  • Maniero, my doubt has arisen due to ";" end with parentheses after keys.

  • It is to finalize the statement of the declaration of lambda.

  • I double-checked the answer and it wasn’t. This will be the third.

1

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

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

Amounts to this:

public int X => A + B;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.