C # basic learning notes -- constants of c,
Constant ".
The numbers (such as 12.85), characters (such as 'F'), and strings (such as "thank you") that we usually write all belong toLiteral constant".
Some constants are both important and error-prone. For example, the circumference rate π value is 3. 1415926..., So we often useCustom constant. For example:
1 1 namespace Test 2 2 {3 3 class Program 4 4 {5 5 static void Main (string [] args) 6 6 {7 const double PI = 3.1415926; // custom constant PI, indicating the circumference rate of 8 Console. write ("the circumference of the circle with a radius of 4 is:"); // String constant 9 9 Console. writeLine (2 * PI * 4); // application custom constant PI10 10 Console. write ("the area of the circle with a radius of 4 is:"); // String constant 11 11 Console. writeLine (PI * 4*4); // application custom constant PI12 12} 13 13} 14 14}
The const keyword indicates that PI is a constant, and the double keyword indicates that PI is of the double Precision Floating Point type (a high precision numeric type ).
This Code uses the circumference rate (perimeter, Area) twice, but because the custom constant is used, the literal constant 3.1415926 is written only once. This avoids errors caused by repeated writing.
Running effect:
Note: constants are assigned values during declaration and cannot be modified later.
The above is taken from MOOC course C # getting started with development
Using constants seems to allow you to set the role identity in the game, for example:
1 namespace Test 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 const string CITY = "a city"; // constant, city 8 const string NAME = "Kevin"; // constant, NAME 9 Console. writeLine (NAME + "a common family born in" + CITY + "); // use constant 10} 11} 12}
The running result is:
Kevin was born into a common family in a city.