using System; public class Starter { public static void Main() { Employee obj1 = new Employee(5678); Employee obj2 = (Employee)obj1.Clone(); if (Employee.ReferenceEquals(obj1, obj2)) { Console.WriteLine("objects identical"); } else { Console.WriteLine("objects not identical"); } } } class Employee : ICloneable { public Employee(int id) { if ((id < 1000) || (id > 9999)) { throw new Exception( "Invalid Employee ID"); } } public object Clone() { return MemberwiseClone(); } }
Class Interface
Object Copy and Clone
using System; public class Name { public string firstName; public string lastName; public Name(string firstName, string lastName) { this.firstName = firstName; this.lastName = lastName; } public void Display() { Console.WriteLine("firstName = " + firstName); Console.WriteLine("lastName = " + lastName); } public static Name Copy(Name car) { return (Name) car.MemberwiseClone(); } } class Test { public static void Main() { Name myName = new Name("T", "M"); Name myOtherName = new Name("P", "B"); Console.WriteLine("myName details:"); myName.Display(); Console.WriteLine("myOtherName details:"); myOtherName.Display(); // perform a memberwise clone of myName using the Name.Copy() method Console.WriteLine("Performing a memberwise clone of myName to myOldName"); Name myOldName = Name.Copy(myName); Console.WriteLine("myOldName details:"); myOldName.Display(); } }
Variables of type object can accept values of any data type
using System; class ObjectTest { public int i = 10; } class MainClass2 { static void Main() { object a; a = 1; // an example of boxing Console.WriteLine(a); Console.WriteLine(a.GetType()); Console.WriteLine(a.ToString()); a = new ObjectTest(); ObjectTest classRef; classRef = (ObjectTest)a; Console.WriteLine(classRef.i); } }
Casting objects: downcast
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); // create a AMD object AMD myAMD = new AMD("V-Rod", true); // cast myAMD to CPU (upcast) CPU myCPU2 = (CPU) myAMD; // cast myCPU2 to AMD (downcast) AMD myAMD2 = (AMD) myCPU2; // myMotorCycle2 has access to all members of the AMD class Console.WriteLine("myAMD2.model = " + myAMD2.model); Console.WriteLine("myAMD2.sidecar = " + myAMD2.sidecar); myAMD2.Start(); myAMD2.PullWheelie(); } }
Casting objects: upcast
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(); } }
This code raises an exception at run time because of an invalid cast
using System; public class Starter { public static void Main() { MyClass obj = new MyClass(); // Fails at compile time // YClass alias=obj; // Fails at run time YClass alias = (YClass)obj; obj.MethodA(); obj.MethodB(); } } public class MyClass { public virtual void MethodA() { } public virtual void MethodB() { } } public class YClass : MyClass { public override void MethodA() { } }
Downcast will fail.
class Employee { } class ContractEmployee : Employee { } class CastExample3 { public static void Main () { ContractEmployee ce = (ContractEmployee)new Employee(); } }