【发布时间】:2014-02-20 18:24:57
【问题描述】:
我已经掌握了属性的基础知识,但我认为它们没有真正的用途。它不只是返回方程的值吗?我的意思是,如果你可以为它写一个简单的等式,那么使用一个属性是没有意义的。
例如:
int currentValue;
public int CurrentValue
{
get { return currentValue; }
set { currentValue = value; }
}
和刚才一样:
currentValue;
另一个例子:
int currentValue;
public int CurrentValue
{
get { return currentValue * 5; }
set { currentValue = value; }
}
与以下内容相同:
currentValue = currentValue * 5;
【问题讨论】:
-
我不确定我是否在关注你,或者你没有基础知识:)。
-
你的意思是“不就是和暴露字段一样吗?”答案是否定的,原因有很多。见csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx
-
+1 为
"..properties, but I don't see a real use for them." -
@gleng 私人二传手有时会派上用场。
-
@JerkeyTurkey 对不起,你刚刚被 Skeet'd。 Properties Matter,作者 Jon Skeet。
标签: c# properties