Getting Started with C # basics-A constant explanation

Source: Internet
Author: User
Tags value of pi
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.

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.