illustrates casting objects


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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example7_11.cs illustrates casting objects
*/

using System;


// declare the MotorVehicle class (the base class)
class MotorVehicle
{

  public string model;

  public MotorVehicle(string model)
  {
    this.model = model;
  }

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

}


// declare the Car class
class Car : MotorVehicle
{

  public bool convertible;

  public Car(string model, bool convertible) :
  base(model)
  {
    this.convertible = convertible;
  }

}


// declare the Motorcycle class
class Motorcycle : MotorVehicle
{

  public bool sidecar;

  // define a constructor
  public Motorcycle(string model, bool sidecar) :
  base(model)
  {
    this.sidecar = sidecar;
  }

  public void PullWheelie()
  {
    Console.WriteLine(model + " pulling a wheelie!");
  }

}


public class Example7_11
{

  public static void Main()
  {

    // create a Car object
    Car myCar = new Car("MR2", true);

    // cast myCar to MotorVehicle (upcast)
    MotorVehicle myMotorVehicle = (MotorVehicle) myCar;

    // myMotorVehicle only has a model field and Start() method
    // (no convertible field)
    Console.WriteLine("myMotorVehicle.model = " + myMotorVehicle.model);
    myMotorVehicle.Start();
    // Console.WriteLine("myMotorVehicle.convertible = " +
    //   myMotorVehicle.convertible);

    // create a Motorcycle object
    Motorcycle myMotorcycle = new Motorcycle("V-Rod", true);

    // cast myMotorcycle to MotorVehicle (upcast)
    MotorVehicle myMotorVehicle2 = (MotorVehicle) myMotorcycle;

    // myMotorVehicle only has a model field and Start() method
    // (no sidecar field or PullWheelie() method)
    Console.WriteLine("myMotorVehicle2.model = " + myMotorVehicle2.model);
    myMotorVehicle2.Start();
    // Console.WriteLine("myMotorVehicle2.sidecar = " +
    //   myMotorVehicle2.sidecar);
    // myMotorVehicle2.PullWheelie();

    // cast myMotorVehicle2 to Motorcycle (downcast)
    Motorcycle myMotorcycle2 = (Motorcycle) myMotorVehicle2;

    // myMotorCycle2 has access to all members of the Motorcycle class
    Console.WriteLine("myMotorcycle2.model = " + myMotorcycle2.model);
    Console.WriteLine("myMotorcycle2.sidecar = " + myMotorcycle2.sidecar);
    myMotorcycle2.Start();
    myMotorcycle2.PullWheelie();

    // cannot cast a Motorcyle object to the Car class because
    // their classes are not compatible
    // Car myCar2 = (Car) myMotorVehicle2;

  }

}


           
         
    
     


Use an explicit conversion


   
  
/*
C#: The Complete Reference 
by Herbert Schildt 

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


// Use an explicit conversion. 
 
using System; 
 
// A three-dimensional coordinate class. 
class ThreeD { 
  int x, y, z; // 3-D coordinates   
 
  public ThreeD() { x = y = z = 0; } 
  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } 
 
  // Overload binary +. 
  public static ThreeD operator +(ThreeD op1, ThreeD op2) 
  { 
    ThreeD result = new ThreeD(); 
 
    result.x = op1.x + op2.x;  
    result.y = op1.y + op2.y;  
    result.z = op1.z + op2.z;  
 
    return result; 
  } 
 
  // This is now explicit. 
  public static explicit operator int(ThreeD op1) 
  { 
    return op1.x * op1.y * op1.z; 
  } 
   
  // Show X, Y, Z coordinates. 
  public void show() 
  { 
    Console.WriteLine(x + ", " + y + ", " + z); 
  } 
} 
 
public class ThreeDDemo7 { 
  public static void Main() { 
    ThreeD a = new ThreeD(1, 2, 3); 
    ThreeD b = new ThreeD(10, 10, 10); 
    ThreeD c = new ThreeD(); 
    int i; 
 
    Console.Write("Here is a: "); 
    a.show(); 
    Console.WriteLine(); 
    Console.Write("Here is b: "); 
    b.show(); 
    Console.WriteLine(); 
 
    c = a + b; // add a and b together 
    Console.Write("Result of a + b: "); 
    c.show(); 
    Console.WriteLine(); 
 
    i = (int) a; // explicitly convert to int -- cast required 
    Console.WriteLine("Result of i = a: " + i); 
    Console.WriteLine(); 
 
    i = (int)a * 2 - (int)b; // casts required 
    Console.WriteLine("result of a * 2 - b: " + i); 
 
  } 
}


           
         
    
     


An example that uses an implicit conversion operator


   
  
/*
C#: The Complete Reference 
by Herbert Schildt 

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


// An example that uses an implicit conversion operator. 
 
using System; 
 
// A three-dimensional coordinate class. 
class ThreeD { 
  int x, y, z; // 3-D coordinates   
 
  public ThreeD() { x = y = z = 0; } 
  public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } 
 
  // Overload binary +. 
  public static ThreeD operator +(ThreeD op1, ThreeD op2) 
  { 
    ThreeD result = new ThreeD(); 
 
    result.x = op1.x + op2.x;  
    result.y = op1.y + op2.y;  
    result.z = op1.z + op2.z;  
 
    return result; 
  } 
 
  // An implicit conversion from ThreeD to int. 
  public static implicit operator int(ThreeD op1) 
  { 
    return op1.x * op1.y * op1.z; 
  } 
   
  // Show X, Y, Z coordinates. 
  public void show() 
  { 
    Console.WriteLine(x + ", " + y + ", " + z); 
  } 
} 
 
public class ThreeDDemo5 { 
  public static void Main() { 
    ThreeD a = new ThreeD(1, 2, 3); 
    ThreeD b = new ThreeD(10, 10, 10); 
    ThreeD c = new ThreeD(); 
    int i; 
 
    Console.Write("Here is a: "); 
    a.show(); 
    Console.WriteLine(); 
    Console.Write("Here is b: "); 
    b.show(); 
    Console.WriteLine(); 
 
    c = a + b; // add a and b together 
    Console.Write("Result of a + b: "); 
    c.show(); 
    Console.WriteLine(); 
 
    i = a; // convert to int 
    Console.WriteLine("Result of i = a: " + i); 
    Console.WriteLine(); 
 
    i = a * 2 - b; // convert to int 
    Console.WriteLine("result of a * 2 - b: " + i); 
  } 
}


           
         
    
     


Implicit cast

   
 

using System;
class ImplicitConversion {
    public static void Main() {
        byte a = 1;
        int b = 1234;
        int c = a; //Implicit cast
        double d = b; //Implicit cast
        Console.WriteLine("{0}", c);
        Console.WriteLine("{0}", d);
    }
}

    


Any object, value, or reference type that is convertible to an integral, char, enum, or string type is acceptable as the switch_expression

   
 

using System;
class Employee {
    public Employee(string f_Emplid) {
        m_Emplid = f_Emplid;
    }

    static public implicit operator string(Employee f_this) {
        return f_this.m_Emplid;
    }

    private string m_Emplid;
}

class Starter {
    static void Main() {
        Employee newempl = new Employee("1234");
        switch (newempl) {
            case "1234":
                Console.WriteLine("Employee 1234");
                return;
            case "5678":
                Console.WriteLine("Employee 5678");
                return;
            default:
                Console.WriteLine("Invalid employee");
                return;
        }
    }
}

    


Conversions between Simple Types

   
 

using System;

public class MainClass {
    public static void Main() {

        double d = 2.9;
        Console.WriteLine((int)d);                   // double-->int; prints 2 
        Console.WriteLine((int)(-d));                // double-->int; prints -2 
        uint seconds = (uint)(24 * 60 * 60);         // int-->uint 
        double avgSecPerYear = 365.25 * seconds;     // I uint-->double 
        float f = seconds;                           // IL uint-->float 
        long nationalDebt1 = 99999999999999;
        double perSecond = 99999.14;
        decimal perDay = seconds * (decimal)perSecond;              // I uint-->decimal 
        double nd2 = nationalDebt1 + (double)perDay; // decimal-->double 
        long nd3 = (long)nd2;                        // double-->long 
        float nd4 = (float)nd2;                      // double-->float 

    }

}