This paper mainly introduces the relevant knowledge of the constants in C #, and has a good reference value. Let's take a look at the little series.
Constants, as the name implies, are "quantities that do not change".
The numbers we normally write (such as 12.85), characters (such as ' F '), strings (such as "Thank You"), are all "literal constants".
Some constants are important and error-prone, such as the value of Pi pi is 3.1415926 ..., so we often use custom constants. Such as:
Namespace Test {class Program { static void Main (string[] args) { const double PI = 3.1415926;//Custom constant pi, representing pi Console.Write ("The circumference of a circle with a radius of 4 is:");//String constant Console.WriteLine (2*pi*4);//Apply custom constant PI Console.Write ("The area of a circle with a radius of 4 is:");//String constant Console.WriteLine (pi*4*4); /Apply custom Constant Pi}}}
The Const keyword indicates that the pi is a constant, and the double keyword indicates that the type of pi is "double-precision floating point" (a high-precision numeric type).
This code uses 2 times pi (perimeter, area), but because of the use of custom constants, the literal constant 3.1415926 is only written once. Doing so avoids errors caused by repeated writing.
Operating effect:
It is also important to note that constants are assigned at the time of declaration and cannot be modified thereafter.
The use of constants, it seems, can achieve the role of the game in the identity of the set, such as:
Namespace test{class Program {static void Main (string[] args) { const string city = "a town";//constant, City const string N AME = "Kevin";//constant, name Console.WriteLine (name+ "Born in" +city+ ");//Use Constant}}}
The result of the operation is:
Kevin was born in an ordinary family in a city.