The use of readonly fields : readonly : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » readonly »

 

The use of readonly fields









    

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example6_3.cs illustrates the use of readonly fields
*/


// declare the Car class
class Car
{

  // declare a readonly field
  public readonly string make;

  // declare a static readonly field
  public static readonly int wheels = 4;

  // define a constructor
  public Car(string make)
  {
    System.Console.WriteLine("Creating a Car object");
    this.make = make;
  }

}


public class Example6_3
{

  public static void Main()
  {

    System.Console.WriteLine("Car.wheels = " + Car.wheels);
    // Car.wheels = 5;  // causes compilation error

    // create a Car object
    Car myCar = new Car("Toyota");

    System.Console.WriteLine("myCar.make = " + myCar.make);
    // myCar.make = "Porsche";  // causes compilation error

  }

}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» readonly