Casting objects : Object Cast : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » Object Cast »

 

Casting objects









    


using System;
   
public class CPU {
  public string model;
   
  public CPU(string model) {
    this.model = model;
  }
   
  public void Start() {
    Console.WriteLine(model + " started");
  }
}
   
public class Intel : CPU {
  public bool convertible;
   
  public Intel(string model, bool convertible) : base(model) {
    this.convertible = convertible;
  }
}
   
public class AMD : CPU {
  public bool sidecar;
   
  public AMD(string model, bool sidecar) : base(model) {
    this.sidecar = sidecar;
  }
   
  public void PullWheelie() {
    Console.WriteLine(model + " pulling a wheelie!");
  }
   
}
   
   
class Test {
  public static void Main() {
    Intel myIntel = new Intel("MR2", true);
   
    // cast myIntel to CPU (upcast)
    CPU myCPU = (CPU) myIntel;
   
    Console.WriteLine("myCPU.model = " + myCPU.model);
    myCPU.Start();
  }
}
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» Object Cast