自动财产默认值 [英] automatic property with default value

查看:119
本文介绍了自动财产默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
你如何给一个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已经得到了它:的 http://geekswithblogs.net/WinAZ/archive/2015/06/ 30 / whatrsquos-新的-C-6.0自动财产initializers.aspx

UPDATE: C# 6 has got it: http://geekswithblogs.net/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.

这篇关于自动财产默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆