The static readonly is the same as the const variable, regardless of whether the access modifier is public or otherwise (private, protected, internal), the variable name is generally uppercase, and the middle is underlined.
public static readonly int MAX_HEIGHT;
public const int MIN_HEIGHT = 10;
Some programmers are not sensitive to uppercase, in the example, Max_height with Max_height, or even maxheight can be replaced. In the. NET class library, int. MaxValue and int.minvalue are defined in this way.
A const constant is more precisely a compilation constant because it does not exist at run time, and all variable references in the compilation are replaced by the actual values. The static readonly is not, and it exists at runtime. In principle, the efficiency of the const is better than static readonly. However, in a comparison project, when a DLL is locally upgraded, if the value of a const variable is changed, and if the DLL that does not upgrade has this const variable, it is obvious that the problem will arise. If you upgrade all DLLs, it is not worth it. Therefore, it is recommended to use static readonly instead of const in large and variable applications. Its negligible efficiency impairment contrasts with the possibility that the upgrade arrangement may appear to be acceptable.
In addition to the above two static read-only and constant variables, the other variable names are underlined, the access modifier is private (not recommended named internal, protected, and not recommended for public):
private static int _maxHeight;
private int _minHeight; 如果其命名不前置下划线,易与参数变量混淆。
对于下面这种定义:
private int _minHeight = default(int);
public int MinHeight
{
set{
_minHeight = value;
}
get{
return _minHeight;
}
}
Beginners may feel a little superfluous, rather than directly named:
public int minheight; This is not easy, why the getter and setter packaging, additional function calls also make the efficiency of the damage.
Sometimes in the development of projects, the beginning of what we want to draw may be just a snake, but the project later demand changed, painting one-stop. So it's a visionary to add a pair of feet to the beginning of a project when it comes to painting snakes.
Getter and Setter (property accessors) can encapsulate logic as a method and use it like a variable, recommending all non-static read-only and constants, defined as private, and then adding a corresponding property accessor to it for assignment and reading. Direct read-write variables are not recommended in other methods (inclusion classes and classes). Even though it can now be read and written directly, we also call it by invoking the property accessor. It's a bit of a hassle, but it's important, and a good veteran sometimes makes mistakes. As shown below:
private int _minHeight = int.MinValue;
public int MinHeight//或者是protected、internal,甚至是private
{
set{
_minHeight = value;
//即使这里目前没有其它处理逻辑
}
get{
return _minHeight;
}
}
public void Method1(int minHeight)
{
this.MinHeight = minHeight;//在这里不要使用this._minHeight直接读写
//
}
Use the property accessor, even if the variable's access is protected or private.
The principle is that for a read of a variable, it is encapsulated with a property accessor, regardless of its access modifier, even though there is currently no other logic in the property accessor except for access.