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

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

问题描述

可能的重复:
公共字段与自动属性

我想这会在某个地方得到解答,但我在通常的地方找不到它.我想知道,这样做有什么好处

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;}}

public int foo {get; set;}

刚刚

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?

推荐答案

这个问题在这里被问了很多次.这些案例没有明确的利益证据(财产内部没有逻辑).会说更多,使用一个字段,您可以获得一些最小的(纳米)速度优势,但与 99.99% 的情况无关.

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.

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

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天全站免登陆