/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Call a hidden method. using System; class A { public int i = 0; // show() in A public void show() { Console.WriteLine("i in base class: " + i); } } // Create a derived class. class B : A { new int i; // this i hides the i in A public B(int a, int b) { base.i = a; // this uncovers the i in A i = b; // i in B } // This hides show() in A. Notice the use of new. new public void show() { base.show(); // this calls show() in A // this displays the i in B Console.WriteLine("i in derived class: " + i); } } public class UncoverName123 { public static void Main() { B ob = new B(1, 2); ob.show(); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Using base to overcome name hiding
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Using base to overcome name hiding. using System; class A { public int i = 0; } // Create a derived class. class B : A { new int i; // this i hides the i in A public B(int a, int b) { base.i = a; // this uncovers the i in A i = b; // i in B } public void show() { // this displays the i in A. Console.WriteLine("i in base class: " + base.i); // this displays the i in B Console.WriteLine("i in derived class: " + i); } } public class UncoverName1231 { public static void Main() { B ob = new B(1, 2); ob.show(); } }
An example of inheritance-related name hiding
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // An example of inheritance-related name hiding. using System; class A { public int i = 0; } // Create a derived class. class B : A { new int i; // this i hides the i in A public B(int b) { i = b; // i in B } public void show() { Console.WriteLine("i in derived class: " + i); } } public class NameHiding { public static void Main() { B ob = new B(2); ob.show(); } }
Four layers of class hierarchy
/* * 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 MyMainClass3 { static void Main(string[] args) { //The function called is based //upon the type called by new. A MyA = new D(); B MyB = new C(); MyA.Display(); //Calls D Display MyB.Display(); //Calls C Display // followed by B's Display //via the base keyword } } class A { public virtual void Display() { Console.WriteLine("Class A's Display Method"); } } class B: A { public override void Display() { Console.WriteLine("Class B's Display Method"); } } class C: B { public override void Display() { Console.WriteLine("Class C's Display Method"); base.Display(); } } class D: C { public override void Display() { Console.WriteLine("Class D's Display Method"); } } }
Inheritance 3
/* * 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 InheritanceChapter_5___Building_Your_Own_Classes { static void Main(string[] args) { B MyB = new D(); D MyD = new D(); //Both result in in D's instance of Display being //called MyB.Display(); MyD.Display(); } } public class B { public virtual void Display() { Console.WriteLine("Class B's Display Method"); } } public class C: B { public override void Display() { Console.WriteLine("Class C's Display Method"); } } public class ContainedClass { int MyInt = 0; } public class D: C { public ContainedClass MyClass = new ContainedClass(); public override void Display() { Console.WriteLine("Class D's Display Method"); } } }
Demonstrates deriving a new class from a base class in another assembly
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Access1.cs - demonstrates deriving a new class from a base class in // another assembly. Also demonstrates how a derived class // may provide a public property to expose a protected member // of a base class. // // Compile this program with the following command line: // C:>csc /r:access.exe Access1.cs // namespace nsAccess { using System; public class Access1 { static public void Main () { clsDerived derived = new clsDerived (); derived.AccessIt = 42; derived.ShowField (); } } // // Derive a class from the base class and give it a public // property to access the private field in the base class class clsDerived : clsBase { public int AccessIt { get {return (Private);} set {Private = value;} } } public class clsBase { private int m_Private; protected int Private { get {return (m_Private);} set {m_Private = value;} } public void ShowField () { Console.WriteLine ("The value of private field m_Private is " + m_Private); } } }
the overridden methods of the System.Object class
using System; using System.Collections; public class Starter { public static void Main() { Employee obj1 = new Employee(5678); Employee obj2 = new Employee(5678); if (obj1 == obj2) { Console.WriteLine("equals"); } else { Console.WriteLine("not equals"); } } } class Employee { public Employee(int id) { if ((id < 1000) || (id > 9999)) { throw new Exception( "Invalid Employee ID"); } propID = id; } public static bool operator ==(Employee obj1, Employee obj2) { return obj1.Equals(obj2); } public static bool operator !=(Employee obj1, Employee obj2) { return !obj1.Equals(obj2); } public override bool Equals(object obj) { Employee _obj = obj as Employee; if (obj == null) { return false; } return this.GetHashCode() == _obj.GetHashCode(); } public override int GetHashCode() { return EmplID; } public string FullName { get { return propFirst + " " + propLast; } } private string propFirst; public string First { get { return propFirst; } set { propFirst = value; } } private string propLast; public string Last { get { return propLast; } set { propLast = value; } } private readonly int propID; public int EmplID { get { return propID; } } public override string ToString() { return FullName; } }