Declare class and use it


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System; 
 
class Rect { 
  public int width; 
  public int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 
} 
  
public class UseRect { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
    Rect r2 = new Rect(7, 9); 
 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine("Area of r2: " + r2.area()); 
 
  } 
}


           
          


Assign value to class


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// This program will not compile. 
 
class X { 
  int a; 
 
  public X(int i) { a = i; } 
} 
 
class Y { 
  int a; 
 
  public Y(int i) { a = i; } 
} 
 
public class IncompatibleRef { 
  public static void Main() { 
    X x = new X(10); 
    X x2;  
    Y y = new Y(5); 
 
    x2 = x; // OK, both of same type 
 
    x2 = y; // Error, not of same type 
  } 
}


           
          


Declaring Class Instances

   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
  public class DeclaringClassInstances
  {
    static void Main(string[] args)
    {
      ClassInstantied MyClass = new ClassInstantied();
    }
  }
  class ClassInstantied
  {
    public void Display()
    {
      Console.WriteLine("Hello World");
    }
  }
}

           
          


Declaring and Defining Classes

   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
  public class DeclaringandDefiningClasses
  {
    static private int MyInt = 5;
    static public int MyInt2 = 10;
    static public int[] MyIntArray;
    static private int ObjectCount = 0;
    static void Main(string[] args)
    {
      MyIntArray = new int[10];
      ObjectCount++;
    }
    public static int MyMethod(int myInt)
    {
      MyInt = MyInt + myInt;
      return MyInt;
    }
    private static long MyLongMethod(ref int myInt)
    {
      return myInt;
    }
  }
}

           
          


A simple inventory example


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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

// A simple inventory example. 
 
using System; 
using System.Collections; 
 
class Inventory { 
  string name; 
  double cost; 
  int onhand; 
 
  public Inventory(string n, double c, int h) { 
    name = n; 
    cost = c; 
    onhand = h; 
  } 
 
  public override string ToString() { 
    return 
      String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}", 
                    name, cost, onhand); 
  } 
} 
 
public class InventoryList { 
  public static void Main() { 
    ArrayList inv = new ArrayList(); 
     
    // Add elements to the list 
    inv.Add(new Inventory("Pliers", 5.95, 3)); 
    inv.Add(new Inventory("Wrenches", 8.29, 2));    
    inv.Add(new Inventory("Hammers", 3.50, 4)); 
    inv.Add(new Inventory("Drills", 19.88, 8)); 
 
    Console.WriteLine("Inventory list:"); 
    foreach(Inventory i in inv) { 
      Console.WriteLine("   " + i); 
    } 
  } 
}


           
          


Using Initializers

   
 


public class Product {
    public string make = "Ford";
    public string model = "T";
    public string color;  // default value of null
    public int yearBuilt = 1910;

    public void Start() {
        System.Console.WriteLine(model + " started");
    }

    public void Stop() {
        System.Console.WriteLine(model + " stopped");
    }
}

class MainClass {

    public static void Main() {
        Product myProduct = new Product();
        System.Console.WriteLine("myProduct.make = " + myProduct.make);
        System.Console.WriteLine("myProduct.model = " + myProduct.model);
        if (myProduct.color == null) {
            System.Console.WriteLine("myProduct.color is null");
        }
        System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);
    }
}

    


simulate a bank account

using System;

public class BankAccount {
public static int nNextAccountNumber = 1000;
public int nAccountNumber;
public double dBalance;

public void InitBankAccount() {
nAccountNumber = ++nNextAccountNumber;
dBalance = 0.0;
}

public void Deposit(double dAmount) {
if (dAmount > 0.0) {
dBalance += dAmount;
}
}

public double Withdraw(double dWithdrawal) {
if (dBalance <= dWithdrawal) { dWithdrawal = dBalance; } dBalance -= dWithdrawal; return dWithdrawal; } } [/csharp]