Use the final type in Split temporary variables in refactoring.
Since the example is written in Java, I want to find the final modifier in C.
Two results are found: const and readonly.
Let's take a look at the differences between the two:
· Both readonly and const are [1] used to identify constants.
· Const can be used to modify the field of the class or a local variable (local variable), while readonly is only used to modify the field of the class.
· The value of a const constant must be clear and constant during compilation, but the value of a readonly constant is a little different, that is, the value can be compiled at runtime. Of course, it must also comply with the constraint as a constant, that is, the value must be constant.
· A const constant must be assigned a value while being declared, and the value must be fixed and constant during compilation; the readonly constant can choose to assign a fixed value to it during compilation at the same time as needed, or assign the initialization work of its value to the instance constructor) complete. For example:Public readonly stringM_now = datetime. Now. tostring ();, m_now will change with the actual situation during running.
· Const constants belong to the class level rather than the instant object level, and cannot be used together with static, the value of this constant will be shared by all instance objects of the entire class (For details, refer to the remark area later ).
· Readonly constants can be class-level or Instance Object-level, depending on its declaration and implementation of initialization. Readonly can be used in combination with static to specify that the constant belongs to the class level and submit the initialization work to the static constructor) complete (for details about how to declare a readonly constant as a class or instance object level, see the remark area later ).
· The type declared as constant by const must be the following primitive type: sbyte, byte, short, ushort, Int, uint, long, ulong, Char, float, double, float, bool, decimal, String.
· Object, array and structure cannot be declared as const constants.
· In general, the reference type cannot be declared as a const constant, but there is an exception: string. The value of the const constant of the reference type can be string or null. In fact, although the string type is a reference type,. Net has special processing on it. This kind of processing is called the immutable character string, so that the string value has the read-only feature. For the constant character string content, refer to Microsoft. NET Framework programming (revision).
From the above descriptions, we can see that there are three types of constants: class level, instance level, and local variable ). The Difference and implementation between the two are already obvious. I have not seen the local variable before. Let's take a look at what it is:
After reading it, we can see that the variable we used in the method is called a local variable. For example:
Public static void main ()
{
Int J = 30;
Console. writeline (j); // output result: 30
}
In, J is a local variable.
Therefore, it can be summarized as follows:
Local variables:
Public static void main ()
{
Int J = 30;
Console. writeline (j); // output result: 30
}
The local variable is identified as a constant:
Public static void main ()
{
Const Int J = 30;
Console. writeline (j); // output result: 30
}
Instance-level fields:
Public class test
{
Public Int J = 20;
Public static void main ()
{
Console. writeline (new test (). J); // output result: 20
}
}
The fields at the instance level are identified as constants:
Public class test
{
Public const Int J = 20;
Public static void main ()
{
Console. writeline (new test (). J); // output result: 20
}
}
Or:
Public class test
{
Public readonly Int J = 20;
Public static void main ()
{
Console. writeline (new test (). J); // output result: 20
}
}
Or:
Public class test
{
Public readonly Int J;
Public test ()
{
J = 20; // note that the readonly field can only be set through the constructor or class.
}
Public static void main ()
{
Console. writeline (new test (). J); // output result: 20
}
}
Category-level fields:
Public class test
{
Public static Int J = 20;
Public static void main ()
{
Console. writeline (test. J); // output result: 20
}
}
Field identification at the category level is constant:
Public class test
{
Public static readonly Int J = 20;
Public static void main ()
{
Console. writeline (test. J); // output result: 20
}
}