这种表达方式是一样的吗 [英] IS BOTH THIS EXPRESSION IS SAME
本文介绍了这种表达方式是一样的吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友.
我想知道以这种方式声明全局变量是一样的..
1.
hi friends.
i want to know that declaring global variable in this fashion is same or not..
1.
private static string ab;
public static string abc
{
get { return ab; }
set { ab = value; }
}
2.
2.
public static string abc;
推荐答案
是!在这两者之间,您可能需要阅读有关公共变量与属性的文章.
http://www.codinghorror.com/blog/2006/08/properties- vs-public-variables.html [ ^ ]
Yes! In between, you might want to read this article about public variables vs properties.
http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html[^]
对于使用您的代码的外部客户端而言,这可能没有多大区别.但是,将变量封装在属性中可以使您验证其值.例如,如果有一个Age
字段,则可以将其值限制在1到120之间.使用公共变量将不允许您验证其值.
这是一个示例:
To an external client that uses your code, it may not make much difference. But encapsulating your variables in a Property enables you to validate its value. For example, if there is anAge
field, you can restrict its values betweeen 1 and 120 or so. Using public variables will not allow you to validate its values.
Here''s an example:
int _age = 0;
public int Age {
get {
return _age;
}
set {
if (_age < 0 || _age > 120) {
throw new ArgumentException("Valid values for Age is between 0 and 120.");
}
_age = value;
}
}
这篇关于这种表达方式是一样的吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文