/* 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(); } }
Month: February 2011
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; } }
A Simple C# Class
using System; public class ASimpleClass { public static void Main() { Point myPoint = new Point(10, 15); Console.WriteLine("myPoint.x {0}", myPoint.x); Console.WriteLine("myPoint.y {0}", myPoint.y); } } class Point { // constructor public Point(int x, int y) { this.x = x; this.y = y; } // member fields public int x; public int y; }
Uses a class from Example16_3a.cs
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example16_3b.cs uses a class from Example16_3a.cs */ using System; using StringSwitch; // name space define in Example16_3c.cs public class Example16_3b { public static void Main() { string localString; MySwitch s = new MySwitch(); s.inString="abcdef"; s.upper(out localString); Console.WriteLine(localString); } } //=========================================================== /* Example16_3c.cs provides manifest information for Example 16_3 */ using System.Reflection; [assembly: AssemblyTitle("Example 16.3")] [assembly: AssemblyVersion("1.0.0.0")] //=========================================================== /* Example16_3a.cs creates a namespace with a single class */ using System; namespace StringSwitch { class MySwitch { string privateString; public string inString { get { return privateString; } set { privateString = value; } } public void upper(out string upperString) { upperString = privateString.ToUpper(); } } }