Csharp/C Sharp/Class Interface/Class Deriving
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;
}
}