问题描述
我认为这将是在某个地方回答,但我没有在平常的地方找到它。我想知道这样做的好处是什么
private int _foo;
public int foo {get {return _foo;} set {_foo = value;}}
或
public int foo {get;设置;}
刚刚
public int foo;
如果需要更复杂的操作,我可以看到好处,但对于简单的案例有什么好处像这样?
这个问题在这里被问了很多次。
这些情况没有明确的好处证据(属性中没有逻辑)。
会说更多,使用一个字段,您可以获得一些最小(纳米)速度收益,但与99.99%的情况无关。
由MS 定义的准则建议使用属性
,用于代码的可扩展性和可维护性。因此,遵循该指南可以让不熟悉代码的人员更轻松地阅读它。
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.
这篇关于为简单变量设置好处有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!