为简单变量设置好处有什么好处 [英] what is the benefit of get set for simple variables

查看:124
本文介绍了为简单变量设置好处有什么好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

公共字段与自动属性


我认为这将是在某个地方回答,但我没有在平常的地方找到它。我想知道这样做的好处是什么

  private int _foo; 
public int foo {get {return _foo;} set {_foo = value;}}

  public int foo {get;设置;} 

刚刚

  public int foo; 

如果需要更复杂的操作,我可以看到好处,但对于简单的案例有什么好处像这样?

解决方案

这个问题在这里被问了很多次。
这些情况没有明确的好处证据(属性中没有逻辑)。
会说更多,使用一个字段,您可以获得一些最小(纳米)速度收益,但与99.99%的情况无关。



由MS 定义的准则建议使用属性 ,用于代码的可扩展性和可维护性。因此,遵循该指南可以让不熟悉代码的人员更轻松地阅读它。

Possible Duplicate:
Public Fields versus Automatic Properties

I figured this would be answered someplace, but I'm not finding it in the usual places. I was wondering, what is the benefit of doing this

private int _foo;
public int foo {get {return _foo;} set{_foo = value;}}

or

public int foo {get; set;}

over just

public int foo;

I can see the benefit if more complex manipulation was required, but what is the benefit for a simple case like this?

解决方案

This question was asked many times here. There is no clear benefit evidence fo these cases (no logic inside property). Would say more, that using a field, you get some minimal (nano) speed benefit, but not relevant for 99.99% cases.

The guideline defined by MS suggests using properties, for expandibility and maintanability of your code. So following that guideline make easier work for someone that not familiar with the code to reading it.

这篇关于为简单变量设置好处有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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