Simple Inventory Example – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */// A simple inventory example.   using System; using System.Collections;   class Inventory { string name; double cost; int onhand;   public Inventory(string n, double c, int h) { name = n; cost = c; onhand = h; }   […]
Continue reading…

Enjoyed this post? Share it!

 
 

Conversions Between Structs 2 – Class Interface

/* A Programmer’s Introduction to C# (Second Edition) by Eric GunnersonPublisher: Apress L.P. ISBN: 1-893115-62-3 */// 24 – User-Defined Conversions\Conversions Between Structs // copyright 2000 Eric Gunnerson using System; using System.Text; struct RomanNumeral { public RomanNumeral(short value) { if (value > 5000) throw(new ArgumentOutOfRangeException());   this.value = value; } public static explicit operator RomanNumeral( short […]
Continue reading…

Enjoyed this post? Share it!

 
 

Demonstrates Deriving New Class From Base Class In Another Assembly – Class Interface

/* C# Programming Tips & Techniques by Charles Wright, Kris JamsaPublisher: 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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

Illustrates Abstract Classes And Methods – Class Interface

/* Mastering Visual C# .NET by Jason Price, Mike GunderloyPublisher: Sybex; ISBN: 0782129110 */ /* Example7_9.cs illustrates abstract classes and methods */using System;// declare the abstract MotorVehicle class abstract class MotorVehicle { // declare the fields public string make; public string model; // define a constructor public MotorVehicle(string make, string model) { this.make = make; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Illustrates Use Of Various Access Modifiers – Class Interface

/* Mastering Visual C# .NET by Jason Price, Mike GunderloyPublisher: Sybex; ISBN: 0782129110 *//* Example5_10.cs illustrates the use of various access modifiers */// declare the Car class class Car { // declare the fields public string make; protected internal string model; internal string color; protected int horsepower = 150; private int yearBuilt; // define the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Multiple Constructors In Class Definition – Class Interface

/* C# Programming Tips & Techniques by Charles Wright, Kris JamsaPublisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Constrct.cs – Demonstrates the use of multiple constructors // in a class definition. // // Compile this program with the following command line: // C:>csc Constrct.cs // namespace nsConstructor { using System; struct POINT { public […]
Continue reading…

Enjoyed this post? Share it!

 
 

Readonly Fields – Class Interface

  using System; using System.Collections.Generic; using System.Text;class Tire { public static readonly Tire GoodStone = new Tire(90); public static readonly Tire FireYear = new Tire(100); public int manufactureID; public Tire() { } public Tire(int ID) { manufactureID = ID; } }class Employee { public readonly string SSN; public Employee(string empSSN) { SSN = empSSN; } […]
Continue reading…

Enjoyed this post? Share it!

 
 

This Code Raises Exception At Run Time Because Of Invalid Cast – Class Interface

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 : […]
Continue reading…

Enjoyed this post? Share it!

 
 

Add Constructor To Building – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Add a constructor to Building.   using System;   class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants     public […]
Continue reading…

Enjoyed this post? Share it!

 
 

C# Always Creates Structure Instance As Value-Type Variable Even Using New Operator – Class Interface

/* C# Programming Tips & Techniques by Charles Wright, Kris JamsaPublisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // ValType.cs — Demonstrates that C# always creates a structure instance as // a value-type variable even using the new operator. // Compile this program using the following command line: // C:>csc ValType.cs // namespace nsValType […]
Continue reading…

Enjoyed this post? Share it!

 
 

Declaring And Defining Classes – Class Interface

/* * 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 DeclaringandDefiningClasses { static private int MyInt = 5; static public int MyInt2 = 10; static public int[] MyIntArray; static private int ObjectCount = 0; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Demonstrate Bytwos Interface – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ using System; public interface ISeries { int getNext(); // return next number in series void reset(); // restart void setStart(int x); // set starting value }// Implement ISeries. class ByTwos : ISeries { int start; int val;   public […]
Continue reading…

Enjoyed this post? Share it!

 
 

Illustrates How To Override Tostring() Method – Class Interface

/* Mastering Visual C# .NET by Jason Price, Mike GunderloyPublisher: Sybex; ISBN: 0782129110 *//* Example7_7.cs illustrates how to override the ToString() method */using System;// declare the Car class class Car { // declare the fields public string make; public string model; // define a constructor public Car(string make, string model) { this.make = make; this.model […]
Continue reading…

Enjoyed this post? Share it!

 
 

Indexers Don’t Have To Operate On Actual Arrays – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */// Indexers don’t have to operate on actual arrays.   using System;   class PwrOfTwo {   /* Access a logical array that contains the powers of 2 from 0 to 15. */ public int this[int index] { // Compute […]
Continue reading…

Enjoyed this post? Share it!

 
 

Overloading Classes – Class Interface

/* * 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 OverloadingClasses { static void Main(string[] args) { A MyA = new A(); MyA.Display(); MyA.Display(10); } } class A { public void Display() { Console.WriteLine("No […]
Continue reading…

Enjoyed this post? Share it!

 
 

Shows Order In Which Constructors And Destructors Are Called In C# Program – Class Interface

/* C# Programming Tips & Techniques by Charles Wright, Kris JamsaPublisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Order.cs – shows the order in which constructors and destructors // are called in a C# program. // // Compile this program with the following command line: // C:>csc Order.cs // namespace nsOrder { using System; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Use Static Field To Count Instances – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */// Use a static field to count instances.   using System;   class CountInst { static int count = 0;   // increment count when object is created public CountInst() { count++; }   // decrement count when object is […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example Of Operator Overloading – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */// An example of operator overloading.   using System;   // A three-dimensional coordinate class. class ThreeD { int x, y, z; // 3-D coordinates   public ThreeD() { x = y = z = 0; } public ThreeD(int i, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Class Declaration With Public Method That Has Parameter. – Class Interface

using System;public class MyClass { public void DisplayMessage( string courseName ) { Console.WriteLine( "Welcome to the grade book for\n{0}!",courseName ); } } public class MyClassTest { public static void Main( string[] args ) { MyClass myMyClass = new MyClass(); Console.WriteLine( "Please enter the course name:" ); string nameOfCourse = Console.ReadLine(); Console.WriteLine(); myMyClass.DisplayMessage( nameOfCourse ); } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Demonstrate Overloaded Constructor – Class Interface

/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */// Demonstrate an overloaded constructor.   using System;   class MyClass { public int x;   public MyClass() { Console.WriteLine("Inside MyClass()."); x = 0; }   public MyClass(int i) { Console.WriteLine("Inside MyClass(int)."); x = i; }   public MyClass(double d) […]
Continue reading…

Enjoyed this post? Share it!