Convert errflag into a property

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Convert errflag into a property.

using System;

class FailSoftArray {
int[] a; // reference to underlying array
int len; // length of array

bool errflag; // now private

// Construct array given its size.
public FailSoftArray(int size) {
a = new int[size];
len = size;
}

// Read-only Length property.
public int Length {
get {
return len;
}
}

// Read-only Error property.
public bool Error {
get {
return errflag;
}
}

// This is the indexer for FailSoftArray.
public int this[int index] {
// This is the get accessor.
get {
if(ok(index)) {
errflag = false;
return a[index];
} else {
errflag = true;
return 0;
}
}

// This is the set accessor
set {
if(ok(index)) {
a[index] = value;
errflag = false;
}
else errflag = true;
}
}

// Return true if index is within bounds.
private bool ok(int index) {
if(index >= 0 & index < Length) return true; return false; } } // Demonstrate the improved fail-soft array. public class FinalFSDemo { public static void Main() { FailSoftArray fs = new FailSoftArray(5); // use Error property for(int i=0; i < fs.Length + 1; i++) { fs[i] = i*10; if(fs.Error) Console.WriteLine("Error with index " + i); } } } [/csharp]

Add Length property to FailSoftArray

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Add Length property to FailSoftArray.

using System;

class FailSoftArray {
int[] a; // reference to underlying array
int len; // length of array — underlies Length property

public bool errflag; // indicates outcome of last operation

// Construct array given its size.
public FailSoftArray(int size) {
a = new int[size];
len = size;
}

// Read-only Length property.
public int Length {
get {
return len;
}
}

// This is the indexer for FailSoftArray.
public int this[int index] {
// This is the get accessor.
get {
if(ok(index)) {
errflag = false;
return a[index];
} else {
errflag = true;
return 0;
}
}

// This is the set accessor
set {
if(ok(index)) {
a[index] = value;
errflag = false;
}
else errflag = true;
}
}

// Return true if index is within bounds.
private bool ok(int index) {
if(index >= 0 & index < Length) return true; return false; } } // Demonstrate the improved fail-soft array. public class ImprovedFSDemo { public static void Main() { FailSoftArray fs = new FailSoftArray(5); int x; // can read Length for(int i=0; i < fs.Length; i++) fs[i] = i*10; for(int i=0; i < fs.Length; i++) { x = fs[i]; if(x != -1) Console.Write(x + " "); } Console.WriteLine(); // fs.Length = 10; // Error, illegal! } } [/csharp]

A simple property example


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// A simple property example. 
 
using System; 
 
class SimpProp {  
  int prop; // field being managed by myprop 
 
  public SimpProp() { prop = 0; } 
 
  /* This is the property that supports access to 
     the private instance variable prop.  It 
     allows only positive values. */ 
  public int myprop { 
    get { 
      return prop; 
    } 
    set { 
      if(value >= 0) prop = value; 
    }  
  } 
}  
  
// Demonstrate a property. 
public class PropertyDemo {  
  public static void Main() {  
    SimpProp ob = new SimpProp(); 
 
    Console.WriteLine("Original value of ob.myprop: " + ob.myprop); 
 
    ob.myprop = 100; // assign value 
    Console.WriteLine("Value of ob.myprop: " + ob.myprop); 
 
    // Can&#039;t assign negative value to prop 
    Console.WriteLine("Attempting to -10 assign to ob.myprop"); 
    ob.myprop = -10; 
    Console.WriteLine("Value of ob.myprop: " + ob.myprop); 
  } 
}


           
          


enum based attribute

   
 


using System;

public enum RemoteServers
{
    A,
    B,
    C
}
   
public class RemoteObjectAttribute : Attribute
{
    public RemoteObjectAttribute(RemoteServers Server)
    {
        this.server = Server;
    }
   
    protected RemoteServers server;
    public string Server
    {
        get 
        { 
            return RemoteServers.GetName(
                typeof(RemoteServers), this.server);
        }
    }
}
   
[RemoteObject(RemoteServers.C)]
class MyRemotableClass
{
}

class Test
{
    [STAThread]
    static void Main(string[] args)
    {
        Type type = typeof(MyRemotableClass);
        foreach (Attribute attr in type.GetCustomAttributes(true))
        {
            RemoteObjectAttribute remoteAttr = attr as RemoteObjectAttribute;
            if (null != remoteAttr)
            {
                Console.WriteLine(remoteAttr.Server);
            }
        }
    }
}

    


Override Properties

   
 

using System;
using System.Collections;
   
abstract class Employee
{
    protected Employee(int employeeId, int hoursWorked)
    {
        this.employeeId = employeeId;
        HoursWorked = hoursWorked;
    }
   
    protected int employeeId;
    public int EmployeeId
    {
        get { return employeeId; }
    }
   
    protected int HoursWorked;
   
    protected double hourlyCost = -1; // dummy init value
    public abstract double HourlyCost
    { 
        get;
    }
}
   
class ContractEmployee : Employee
{
    public ContractEmployee(int employeeId, double hourlyWage,
        int hoursWorked)
        : base(employeeId, hoursWorked)
    {
        HourlyWage = hourlyWage;
    }
   
    protected double HourlyWage;
    public override double HourlyCost
    { 
        get 
        { return HourlyWage; }
    }
}
   
class SalariedEmployee : Employee
{
    public SalariedEmployee(int employeeId, double salary,
        int hoursWorked)
        : base(employeeId, hoursWorked)
    {
        Salary = salary;
    }
   
    protected double Salary;
    public override double HourlyCost
    { 
        get 
        { 
            return (Salary / 52) / HoursWorked; 
        }
    }
}
   
class OverrideProperties
{
    public static ArrayList employees = new ArrayList();
 
    public static void PrintEmployeesHourlyCostToCompany()
    {
        foreach (Employee employee in employees)
        {
            Console.WriteLine("{0} employee (id={1}) costs {2}" +
                " per hour", employee, employee.EmployeeId,
                employee.HourlyCost);
        }
    }
   
    public static void Main()
    {
        ContractEmployee c  = new ContractEmployee(1, 50, 40);
        employees.Add(c);
   
        SalariedEmployee s =
            new SalariedEmployee(2, 100000, 65);
        employees.Add(s);
   
        PrintEmployeesHourlyCostToCompany();
   
        Console.ReadLine();
    }
}

    


MyClass class that contains a private courseName instance variable, and a property to get and set its value.

   


using System;

public class MyClass
{
   private string courseName;

   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 myMyClass = new MyClass();
      Console.WriteLine( "Initial course name is: &#039;{0}&#039;
",myMyClass.CourseName );
      Console.WriteLine( "Please enter the course name:" );
      string theName = Console.ReadLine();
      myMyClass.CourseName = theName;
      Console.WriteLine();
      myMyClass.DisplayMessage();
   }
}
           
          


Using variable-length argument lists.

   




using System;

public class VarargsTest
{
   public static double Average( params double[] numbers )
   {
      double total = 0.0; // initialize total
      foreach ( double d in numbers )
         total += d;

      return total / numbers.Length;
   } 
   public static void Main( string[] args )
   {
      double d1 = 10.0;
      double d2 = 20.0;
      double d3 = 30.0;
      double d4 = 40.0;

      Console.WriteLine("d1 = {0:F1}
d2 = {1:F1}
d3 = {2:F1}
d4 = {3:F1}
",d1, d2, d3, d4 );

      Console.WriteLine( "Average of d1 and d2 is {0:F1}",Average( d1, d2 ) );
      Console.WriteLine( "Average of d1, d2 and d3 is {0:F1}",Average( d1, d2, d3 ) );
      Console.WriteLine( "Average of d1, d2, d3 and d4 is {0:F1}",Average( d1, d2, d3, d4 ) );
   }
}