/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// This program will not compile.
class X {
int a;
public X(int i) { a = i; }
}
class Y {
int a;
public Y(int i) { a = i; }
}
public class IncompatibleRef {
public static void Main() {
X x = new X(10);
X x2;
Y y = new Y(5);
x2 = x; // OK, both of same type
x2 = y; // Error, not of same type
}
}
Month: February 2011
Declaring Class Instances
/*
* 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 DeclaringClassInstances
{
static void Main(string[] args)
{
ClassInstantied MyClass = new ClassInstantied();
}
}
class ClassInstantied
{
public void Display()
{
Console.WriteLine("Hello World");
}
}
}
Declaring and Defining Classes
/*
* 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;
static void Main(string[] args)
{
MyIntArray = new int[10];
ObjectCount++;
}
public static int MyMethod(int myInt)
{
MyInt = MyInt + myInt;
return MyInt;
}
private static long MyLongMethod(ref int myInt)
{
return myInt;
}
}
}
A simple inventory example
/*
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;
}
public override string ToString() {
return
String.Format("{0,-10}Cost: {1,6:C} On hand: {2}",
name, cost, onhand);
}
}
public class InventoryList {
public static void Main() {
ArrayList inv = new ArrayList();
// Add elements to the list
inv.Add(new Inventory("Pliers", 5.95, 3));
inv.Add(new Inventory("Wrenches", 8.29, 2));
inv.Add(new Inventory("Hammers", 3.50, 4));
inv.Add(new Inventory("Drills", 19.88, 8));
Console.WriteLine("Inventory list:");
foreach(Inventory i in inv) {
Console.WriteLine(" " + i);
}
}
}
Using Initializers
public class Product {
public string make = "Ford";
public string model = "T";
public string color; // default value of null
public int yearBuilt = 1910;
public void Start() {
System.Console.WriteLine(model + " started");
}
public void Stop() {
System.Console.WriteLine(model + " stopped");
}
}
class MainClass {
public static void Main() {
Product myProduct = new Product();
System.Console.WriteLine("myProduct.make = " + myProduct.make);
System.Console.WriteLine("myProduct.model = " + myProduct.model);
if (myProduct.color == null) {
System.Console.WriteLine("myProduct.color is null");
}
System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);
}
}
simulate a bank account
using System;
public class BankAccount {
public static int nNextAccountNumber = 1000;
public int nAccountNumber;
public double dBalance;
public void InitBankAccount() {
nAccountNumber = ++nNextAccountNumber;
dBalance = 0.0;
}
public void Deposit(double dAmount) {
if (dAmount > 0.0) {
dBalance += dAmount;
}
}
public double Withdraw(double dWithdrawal) {
if (dBalance <= dWithdrawal) {
dWithdrawal = dBalance;
}
dBalance -= dWithdrawal;
return dWithdrawal;
}
}
[/csharp]
Illustrates the use of various access modifiers
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: 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 methods
public void SetYearBuilt(int yearBuilt)
{
this.yearBuilt = yearBuilt;
}
public int GetYearBuilt()
{
return yearBuilt;
}
public void Start()
{
System.Console.WriteLine("Starting car ...");
TurnStarterMotor();
System.Console.WriteLine("Car started");
}
private void TurnStarterMotor()
{
System.Console.WriteLine("Turning starter motor ...");
}
}
public class Example5_10
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
// assign values to the Car object fields
myCar.make = "Toyota";
myCar.model = "MR2";
myCar.color = "black";
// myCar.horsepower = 200; // protected field not accessible
// myCar.yearBuilt = 1995; // private field not accessible
// call the SetYearBuilt() method to set the private yearBuilt field
myCar.SetYearBuilt(1995);
// display the values for the Car object fields
System.Console.WriteLine("myCar.make = " + myCar.make);
System.Console.WriteLine("myCar.model = " + myCar.model);
System.Console.WriteLine("myCar.color = " + myCar.color);
// call the GetYearBuilt() method to get the private yearBuilt field
System.Console.WriteLine("myCar.GetYearBuilt() = " + myCar.GetYearBuilt());
// call the Start() method
myCar.Start();
// myCar.TurnStarterMotor(); // private method not accessible
}
}

