老实说,什么是公共变量和公共财产的访问有什么区别? [英] Honestly, what's the difference between public variable and public property accessor?

查看:172
本文介绍了老实说,什么是公共变量和公共财产的访问有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能显示的文件:结果
是什么的场和在C#的结果,
<属性之间的差A HREF =HTTP:/ /stackoverflow.com/questions/1277572/should-i-use-public-properties-and-private-fields-or-public-fields-for-data\">Should我用数据公共属性和私人领域和公共领域?






之间有什么区别:

 公共字符串翻; 

 公共字符串翻{搞定;组; } 


解决方案

公共属性访问器为您提供了更多的灵活性未来。



如果您想验证添加到设定值,你只要写一个非默认的制定者。您的其他代码无将不得不进行修改。



有也可能是你要替换的代码默认吸气原因。这可以是一个公共变量一个真正的痛苦。


Possible Duplicates:
What is the difference between a field and a property in C#
Should I use public properties and private fields or public fields for data?

What is the difference between:

public string varA;

and

public string varA { get; set; }

解决方案

The public property accessor gives you more flexibility in the future.

If you want to add validation to setting the value, you simply write a non-default setter. None of your other code would have to be modified.

There could also be reasons you'd want to replace the default getter with code. That can be a real pain with a public variable.

这篇关于老实说,什么是公共变量和公共财产的访问有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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