Finalizable Disposable Class

using System;
using System.Collections.Generic;
using System.Text;

class Program {
static void Main(string[] args) {
using (MyResourceWrapper rw = new MyResourceWrapper()) {
}
MyResourceWrapper rw2 = new MyResourceWrapper();
for (int i = 0; i < 10; i++) rw2.Dispose(); } } public class MyResourceWrapper : IDisposable { public void Dispose() { Console.WriteLine("In Dispose() method!"); } } [/csharp]

C# Class Constructor Overloading

   

public Overloading
{
    public static void Main()
    {
        Point myPoint = new Point(10, 15);
        Point mySecondPoint = new Point(myPoint);
    }
}

class Point
{
    // create a new point from x and y values
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    // create a point from an existing point
    public Point(Point p)
    {
        this.x = p.x;
        this.y = p.y;
    }
    
    int x;
    int y;
}


           
          


Add a constructor to Building


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add a constructor to Building. 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
 
  public Building(int f, int a, int o) { 
    floors = f; 
    area = a; 
    occupants = o; 
  } 
 
  // Display the area per person.  
  public int areaPerPerson() {  
    return area / occupants; 
  }  
 
  /* Return the maximum number of occupants if each 
     is to have at least the specified minum area. */ 
  public int maxOccupant(int minArea) { 
    return area / minArea; 
  } 
}   
   
// Use the parameterized Building constructor. 
public class BuildingDemo21 {   
  public static void Main() {   
    Building house = new Building(2, 2500, 4);   
    Building office = new Building(3, 4200, 25); 
 
    Console.WriteLine("Maximum occupants for house if each has " + 
                      300 + " square feet: " + 
                      house.maxOccupant(300)); 
 
    Console.WriteLine("Maximum occupants for office if each has " + 
                      300 + " square feet: " + 
                      office.maxOccupant(300)); 
  }   
}

           
          


A parameterized constructor


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A parameterized constructor. 
 
using System; 
 
class MyClass { 
  public int x; 
 
  public MyClass(int i) { 
    x = i; 
  }   
}   
   
public class ParmConsDemo {   
  public static void Main() {   
    MyClass t1 = new MyClass(10); 
    MyClass t2 = new MyClass(88); 
 
    Console.WriteLine(t1.x + " " + t2.x); 
  }   
}

           
          


A simple constructor


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple constructor. 
 
using System; 
 
class MyClass { 
  public int x; 
 
  public MyClass() { 
    x = 10; 
  }   
}   
   
public class ConsDemo1 {   
  public static void Main() {   
    MyClass t1 = new MyClass(); 
    MyClass t2 = new MyClass(); 
 
    Console.WriteLine(t1.x + " " + t2.x); 
  }   
}

           
          


Illustrates a copy constructor


   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_13.cs illustrates a copy constructor
*/


// declare the Car class
class Car
{

  // declare the fields
  private string make;
  private string model;
  private string color;
  private int yearBuilt;

  // define the copy constructor
  public Car(Car car)
  {
    this.make = car.make;
    this.model = car.model;
    this.color = car.color;
    this.yearBuilt = car.yearBuilt;
  }

  public Car(string make, string model, string color, int yearBuilt)
  {
    this.make = make;
    this.model = model;
    this.color = color;
    this.yearBuilt = yearBuilt;
  }

  // define method to display the fields
  public void Display()
  {
    System.Console.WriteLine("make = " + make);
    System.Console.WriteLine("model = " + model);
    System.Console.WriteLine("color = " + color);
    System.Console.WriteLine("yearBuilt = " + yearBuilt);
  }

}


public class Example5_13
{

  public static void Main()
  {

    // create a Car object
    Car myCar = new Car("Toyota", "MR2", "black", 1995);

    // create a copy of this Car object
    Car carCopy = new Car(myCar);

    // display the values for the Car object&#039;s fields
    System.Console.WriteLine("myCar details:");
    myCar.Display();
    System.Console.WriteLine("carCopy details:");
    carCopy.Display();

  }

}