Illustrates the use of static members


   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example6_1.cs illustrates the use of static members
*/


// declare the Car class
class Car
{

  // declare a static field,
  // numberOfCars stores the number of Car objects
  private static int numberOfCars = 0;

  // define the constructor
  public Car()
  {
    System.Console.WriteLine("Creating a Car object");
    numberOfCars++;  // increment numberOfCars
  }

  // define the destructor
  ~Car()
  {
    System.Console.WriteLine("Destroying a Car object");
    numberOfCars--;  // decrement numberOfCars
  }

  // define a static method that returns numberOfCars
  public static int GetNumberOfCars()
  {
    return numberOfCars;
  }

}


public class Example6_1
{

  public static void Main()
  {

    // display numberOfCars
    System.Console.WriteLine("Car.GetNumberOfCars() = " +
      Car.GetNumberOfCars());

    // create a Car object
    Car myCar = new Car();
    System.Console.WriteLine("Car.GetNumberOfCars() = " +
      Car.GetNumberOfCars());

    // create another Car object
    Car myCar2 = new Car();
    System.Console.WriteLine("Car.GetNumberOfCars() = " +
      Car.GetNumberOfCars());

  }

}


           
          


Use a static constructor


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use a static constructor. 
 
using System; 
 
class Cons { 
  public static int alpha; 
  public int beta; 
 
  // static constructor 
  static Cons() { 
    alpha = 99; 
    Console.WriteLine("Inside static constructor."); 
  } 
 
  // instance constructor 
  public Cons() { 
    beta = 100; 
    Console.WriteLine("Inside instance constructor."); 
  } 
} 
  
public class ConsDemo { 
  public static void Main() {   
    Cons ob = new Cons(); 
 
    Console.WriteLine("Cons.alpha: " + Cons.alpha); 
    Console.WriteLine("ob.beta: " + ob.beta); 
                  
  } 
}
           
          


Use a static class factory

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Use a static class factory.

using System;

class MyClass {
int a, b;

// Create a class factory for MyClass.
static public MyClass factory(int i, int j) {
MyClass t = new MyClass();

t.a = i;
t.b = j;

return t; // return an object
}

public void show() {
Console.WriteLine(“a and b: ” + a + ” ” + b);
}

}

public class MakeObjects1 {
public static void Main() {
int i, j;

// generate objects using the factory
for(i=0, j=10; i < 10; i++, j--) { MyClass ob = MyClass.factory(i, j); // get an object ob.show(); } Console.WriteLine(); } } [/csharp]

Use a static field to count instances

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Use a static field to count instances.

using System;

class CountInst {
static int count = 0;

// increment count when object is created
public CountInst() {
count++;
}

// decrement count when object is destroyed
~CountInst() {
count–;
}

public static int getcount() {
return count;
}
}

public class CountDemo {
public static void Main() {
CountInst ob;

for(int i=0; i < 10; i++) { ob = new CountInst(); Console.WriteLine("Current count: " + CountInst.getcount()); } } } [/csharp]

Can call a non-static method through an object reference from within a static method

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;

public class MyClass { 
  // non-static method. 
  void nonStaticMeth() { 
     Console.WriteLine("Inside nonStaticMeth()."); 
  } 
 
  /* Can call a non-static method through an 
     object reference from within a static method. */ 
  public static void staticMeth(MyClass ob) { 
    ob.nonStaticMeth(); // this is OK 
  } 
}


           
          


Error using static

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


using System; 
 
public class AnotherStaticError { 
  // non-static method. 
  void nonStaticMeth() { 
     Console.WriteLine("Inside nonStaticMeth()."); 
  } 
 
  /* Error! Can&#039;t directly call a non-static method 
     from within a static method. */ 
  static void staticMeth() { 
    nonStaticMeth(); // won&#039;t compile 
  } 
}


           
          


Static members are frequently used as counters.

   
 

using System;

public class Starter {
    public static void Main() {
        MyClass<int> obj1 = new MyClass<int>();
        MyClass<double> obj2 = new MyClass<double>();
        MyClass<double> obj3 = new MyClass<double>();
        MyClass<int>.Count(obj1);
        MyClass<double>.Count(obj2);
    }
}

public class MyClass<T> {

    public MyClass() {
        ++counter;
    }

    public static void Count(MyClass<T> _this) {
        Console.WriteLine("{0} : {1}",
            _this.GetType().ToString(),
            counter.ToString());
    }

    private static int counter = 0;
}