Set a default value to a property

后端 未结 5 1576
你的背包
你的背包 2021-02-11 21:58

Is it possible to set a default value without the body of a property? Preferably with annotations.

[SetTheDefaultValueTo(true)]
public bool IsTrue { get; set; }
         


        
5条回答
  •  太阳男子
    2021-02-11 22:33

    In this very specific example, you sort of can:

    public bool IsFalse { get; set; }
    public bool IsTrue
    {
        get { return !IsFalse; }
        set { IsFalse = !value; }
    }
    
    public void Something()
    {
        var isTrue = this.IsTrue;
        var isFalse = this.IsFalse;
    }
    

    But, in general, no.

提交回复
热议问题