Set a default value to a property

后端 未结 5 1587
你的背包
你的背包 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:31

    If you are using C#5 and earlier, you have to do it in a constructor.

    but since C# 6.0, the ability to have auto property initializers is included, and the syntax is:

    public int myage { get; set; } = 33;
    

提交回复
热议问题