/* 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); } } }
Class Interface
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(); } } }
Return an object
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Return an object. using System; class Rect { int width; int height; public Rect(int w, int h) { width = w; height = h; } public int area() { return width * height; } public void show() { Console.WriteLine(width + " " + height); } /* Return a rectangle that is a specified factor larger than the invoking rectangle. */ public Rect enlarge(int factor) { return new Rect(width * factor, height * factor); } } public class RetObj { public static void Main() { Rect r1 = new Rect(4, 5); Console.Write("Dimensions of r1: "); r1.show(); Console.WriteLine("Area of r1: " + r1.area()); Console.WriteLine(); // create a rectange that is twice as big as r1 Rect r2 = r1.enlarge(2); Console.Write("Dimensions of r2: "); r2.show(); Console.WriteLine("Area of r2 " + r2.area()); } }
This program creates two Building objects
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // This program creates two Building objects. using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants } // This class declares two objects of type Building. public class BuildingDemo1 { public static void Main() { Building house = new Building(); Building office = new Building(); int areaPP; // area per person // assign values to fields in house house.occupants = 4; house.area = 2500; house.floors = 2; // assign values to fields in office office.occupants = 25; office.area = 4200; office.floors = 3; // compute the area per person in house areaPP = house.area / house.occupants; Console.WriteLine("house has: " + house.floors + " floors " + house.occupants + " occupants " + house.area + " total area " + areaPP + " area per person"); Console.WriteLine(); // compute the area per person in office areaPP = office.area / office.occupants; Console.WriteLine("office has: " + office.floors + " floors " + office.occupants + " occupants " + office.area + " total area " + areaPP + " area per person"); } }
A program that uses the Building class
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A program that uses the Building class. using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants } // This class declares an object of type Building. public class BuildingDemo { public static void Main() { Building house = new Building(); // create a Building object int areaPP; // area per person // assign values to fields in house house.occupants = 4; house.area = 2500; house.floors = 2; // compute the area per person areaPP = house.area / house.occupants; Console.WriteLine("house has: " + house.floors + " floors " + house.occupants + " occupants " + house.area + " total area " + areaPP + " area per person"); } }