Herong's Tutorial Notes On C# - Part B
Dr. Herong Yang, Version 2.02

Classes

Part:   1   2 

"const" and "readonly"

C# has two key words to prevent values of variables to be changed unwantedly: const and readonly. Here is a comparison of the two:

"const"

  • Automatically become "static"
  • Can only be value based types
  • Can only be assigned in the declaration statement
  • Can only be assigned with an expression evaluable at the compiler time

"readonly"

  • Can be "static" or non "static"
  • Can be value or reference based types
  • Can only be assigned in the declaration statement

The following sample program shows some interesting features:

class Constants {
   public const long minimum = 1; // const is also static
   public const long maximum = 100;
   public static readonly long average = 50;
   public static readonly string author = "Herong Yang";
   public static readonly int[] seeds = new int[] {3, 7, 11};
   public static void Main() {
      // minimum = 0; // assignment not allowed
      // maximum = 99;
      // average = 49; // assignment not allowed
      // author = "Someone Else"; // assignment not allowed
      // seeds = new int[] {13, 17, 19}; // can not change the reference
      seeds[0] = 5; // but, we can change value it is referring
      System.Console.WriteLine("Constants.minimum = {0}.", 
         Constants.minimum); 	
      System.Console.WriteLine("Constants.maximum = {0}.", 
         Constants.maximum); 	
      System.Console.WriteLine("Constants.average = {0}.", 
         Constants.average); 	
      System.Console.WriteLine("Constants.author = {0}.", 
         Constants.author);
      System.Console.WriteLine("Constants.seeds[0] = {0}.", 
         Constants.seeds[0]);
   }
}

Output:

Constants.minimum = 1.
Constants.maximum = 100.
Constants.average = 50.
Constants.author = Herong Yang.
Constants.seeds[0] = 5.

Method Overload

A method is overloaded if there is another method with the same method signature existing in the same class. A method signature includs the following elements:

  • Method name.
  • Number of parameters.
  • Parameter type.
  • Parameter modifier, except "params".

and excludes the following elements:

  • Return type.
  • Method modifier.
The following program shows the how a method can be overloaded differently:
class Overloads {
   public static void Main() {
      Print(1);
      Print(12345678901234);
      Print((object)2);
      Print(3, 4);
      Print(new int[]{5,6});
   }
   public static void Print(object o) {
      System.Console.WriteLine("Print(object o): {0}.", o.ToString());
   }
   public static void Print(int i) {
      System.Console.WriteLine("Print(int i): {0}.", i);
   }
   public static void Print(long i) {
      System.Console.WriteLine("Print(long i): {0}.", i);
   }
   public static void Print(ref int i) {
      System.Console.WriteLine("Print(ref int i): {0}.", i);
   }
   public static void Print(int i, int j) {
      System.Console.WriteLine("Print(int i, int j): {0}, {1}.", i, j);
   }
//   public static void Print(int[] a) {
//      // identical to Print(params int[] a)
//      System.Console.WriteLine("Print(int[] a): {0}, {1}.", a[0], 
//         a[1]);
//   }
   public static void Print(params int[] a) {
      System.Console.WriteLine("Print(params int[] a): {0}, {1}.", 
         a[0],a[1]);
   }
}

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2002
Herong's Tutorial Notes On C# - Part B - Classes