可能重复:结果的你如何给一个C#自动属性的默认值?
有没有提供一个自动的财产?
Is there any nice way to provide a default value for an automatic property?
公众诠释HowHigh {搞定;组; } //默认为0
如果没有明确设定的任何地方,我想这是5.你知道它一个简单的方法?例如。我可以设置它的构造函数或什么的,但是这不是优雅
If not explicitly set anywhere, I want it to be 5. Do you know a simple way for it? E.g. I could set it in constructor or something, but that's not elegant.
更新:C#6已经得到了它:的 geekswithblogs/WinAZ/archive/2015/06/ 30 / whatrsquos-新的-C-6.0自动财产initializers.aspx
UPDATE: C# 6 has got it: geekswithblogs/WinAZ/archive/2015/06/30/whatrsquos-new-in-c-6.0-auto-property-initializers.aspx
推荐答案如果该该类型的默认值是不够的,那么这样做是通过一个构造的唯一途径。
If the default value for the type is not sufficient, then the only way to do it is via a constructor.
更多推荐
自动财产默认值
发布评论