Csharp/C Sharp/Language Basics/Constant

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Illustrates the use of a const field

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example6_2.cs illustrates the use of a const field
  • /

// declare the Car class class Car {

 // declare a const field
 public const int wheels = 4;

}

public class Example6_2 {

 public static void Main()
 {
   System.Console.WriteLine("Car.wheels = " + Car.wheels);
   // Car.wheels = 5;  // causes compilation error
 }

}

      </source>


Illustrates the use of constants

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example2_7.cs illustrates the use of constants
  • /

public class Example2_7 {

 public static void Main()
 {
   const int Length = 3;
   // mathematical constant pi
   const double Pi = 3.14159;
   // speed of light in meters per second
   const double SpeedOfLight = 2.99792e8;
   System.Console.WriteLine(Length);
   System.Console.WriteLine(Pi);
   System.Console.WriteLine(SpeedOfLight);
 }

}

      </source>