C # basic learning notes -- constants of c,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.