How to override a getter-only property with a setter in C#?

前端 未结 3 733
闹比i
闹比i 2020-12-17 22:02

Update: This question has been revised to make it clearer. The answers below seem to reflect that this method works well. Hopefully this questio

3条回答
  •  [愿得一人]
    2020-12-17 22:47

    UPDATE: The following is INCORRECT.

    No.

    public abstract class A
    {
        public abstract int X { get; }
    
        public int GetXPlusOne()
        {
            return X + 1;
        }
    }
    

    You won't change the value of A.X.

    var d = new D();
    d.X = 10;
    
    d.GetXPlusOne() == 1
    

提交回复
热议问题