using System;
public class Account
{
private decimal balance;
public Account( decimal initialBalance )
{
Balance = initialBalance;
}
public void Credit( decimal amount )
{
Balance = Balance + amount;
}
public decimal Balance
{
get
{
return balance;
}
set
{
if ( value >= 0 )
balance = value;
}
}
}
public class AccountTest
{
public static void Main( string[] args )
{
Account account1 = new Account( 5.00M );
Account account2 = new Account( -7.53M );
Console.WriteLine( "account1 balance: {0:C}",account1.Balance );
Console.WriteLine( "account2 balance: {0:C}
",account2.Balance );
decimal depositAmount;
Console.Write( "Enter deposit amount for account1: " );
depositAmount = Convert.ToDecimal( Console.ReadLine() );
Console.WriteLine( "adding {0:C} to account1 balance
",depositAmount );
account1.Credit( depositAmount );
Console.WriteLine( "account1 balance: {0:C}",account1.Balance );
Console.WriteLine( "account2 balance: {0:C}
",account2.Balance );
Console.Write( "Enter deposit amount for account2: " );
depositAmount = Convert.ToDecimal( Console.ReadLine() );
Console.WriteLine( "adding {0:C} to account2 balance
",depositAmount );
account2.Credit( depositAmount );
Console.WriteLine( "account1 balance: {0:C}", account1.Balance );
Console.WriteLine( "account2 balance: {0:C}", account2.Balance );
}
}
Month: February 2011
MyClass class with a public constructor to initialize the course name.
using System;
public class MyClass
{
private string courseName;
public MyClass( string name )
{
CourseName = name;
}
public string CourseName
{
get
{
return courseName;
}
set
{
courseName = value;
}
}
public void DisplayMessage()
{
Console.WriteLine( CourseName );
}
}
public class MyClassTest
{
public static void Main( string[] args )
{
MyClass gradeBook1 = new MyClass("C#" );
MyClass gradeBook2 = new MyClass("Data Structures" );
Console.WriteLine( "gradeBook1 course name is: {0}",gradeBook1.CourseName );
Console.WriteLine( "gradeBook2 course name is: {0}",gradeBook2.CourseName );
}
}
Class declaration with a public method that has a parameter.
using System;
public class MyClass
{
public void DisplayMessage( string courseName )
{
Console.WriteLine( "Welcome to the grade book for
{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 );
}
}
Class declaration with one public method.
using System;
public class MyClass
{
public void DisplayMessage()
{
Console.WriteLine( "Welcome to the Grade Book!" );
}
}
public class MyClassTest
{
public static void Main( string[] args )
{
MyClass myMyClass = new MyClass();
myMyClass.DisplayMessage();
}
}
Properties:Virtual Properties
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 18 - PropertiesVirtual Properties
// copyright 2000 Eric Gunnerson
using System;
public abstract class DrawingObject
{
public abstract string Name
{
get;
}
}
class Circle: DrawingObject
{
string name = "Circle";
public override string Name
{
get
{
return(name);
}
}
}
public class PropertiesVirtualProperties
{
public static void Main()
{
DrawingObject d = new Circle();
Console.WriteLine("Name: {0}", d.Name);
}
}
Properties:Static Properties
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 18 - PropertiesStatic Properties
// copyright 2000 Eric Gunnerson
class Color
{
public Color(int red, int green, int blue)
{
this.red = red;
this.green = green;
this.blue = blue;
}
int red;
int green;
int blue;
public static Color Red
{
get
{
return(new Color(255, 0, 0));
}
}
public static Color Green
{
get
{
return(new Color(0, 255, 0));
}
}
public static Color Blue
{
get
{
return(new Color(0, 0, 255));
}
}
}
public class StaticProperties
{
static void Main()
{
Color background = Color.Red;
}
}
Delegates as Static Properties
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 22 – DelegatesDelegates as Static Properties
// copyright 2000 Eric Gunnerson
using System;
class Container
{
public delegate int CompareItemsCallback(object obj1, object obj2);
public void SortItems(CompareItemsCallback compare)
{
// not a real sort, just shows what the
// inner loop code might do
int x = 0;
int y = 1;
object item1 = arr[x];
object item2 = arr[y];
int order = compare(item1, item2);
}
object[] arr; // items in the collection
}
class Employee
{
Employee(string name, int id)
{
this.name = name;
this.id = id;
}
public static Container.CompareItemsCallback SortByName
{
get
{
return(new Container.CompareItemsCallback(CompareName));
}
}
public static Container.CompareItemsCallback SortById
{
get
{
return(new Container.CompareItemsCallback(CompareId));
}
}
static int CompareName(object obj1, object obj2)
{
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;
return(String.Compare(emp1.name, emp2.name));
}
static int CompareId(object obj1, object obj2)
{
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;
if (emp1.id > emp2.id)
return(1);
if (emp1.id < emp2.id)
return(-1);
else
return(0);
}
string name;
int id;
}
public class DelegatesasStaticProperties
{
public static void Main()
{
Container employees = new Container();
// create and add some employees here
employees.SortItems(Employee.SortByName);
// employees is now sorted by name
}
}
[/csharp]

