Return class object from indexer

Thursday, July 9th, 2009

 
using System;

public class MyValue {
    public String Name;
}

class CardDeck {
    private MyValue[] Cards = new MyValue[52];
    public MyValue this[int index] {
        get {
            return Cards[index];
        }
        set {
            Cards[index] = value;
        }
    }

    public static void Main(String[] args) {
        try {
            CardDeck PokerDeck = new CardDeck();
            MyValue HiddenAce = PokerDeck[53];
        } catch (IndexOutOfRangeException e) {
            Console.WriteLine(e.Message);
        } finally {
            // Cleanup code
        }
    }
}

 

Demostrates the use of an abstract class, including an abstract method and abstract properties

Sunday, June 28th, 2009

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// Abstract.cs – Demostrates the use of an abstract class, including
//                an abstract method and abstract properties.
//
//                Compile this program with the following command line:
//                    C:>csc Abstract.cs
//
namespace nsAbstract
{
    using System;
    
    public class AbstractclsMain
    {
        static public void Main ()
        {
            // Create an instance of the derived class.
            clsDerived derived = new clsDerived (3.14159);
            // Calling GetAbstract() actually calls the public method in the
            // base class. There is no GetAbstract() in the derived class.
            derived.GetAbstract();
        }
    }

    // Declare an abstract class

    abstract class clsBase
    {
        // Declare an abstract method. Note the semicolon to end the declaration
        abstract public void Describe();

        // Declare an abstract property that has only a get accessor. 
        // Note that you
        // do not prove the braces for the accessor
        abstract public double DoubleProp
        {
            get;
        }
        // Declare an abstract property that has only a set accessor.
        abstract public int IntProp
        {
            set;
        }
        // Declare an abstract propety that has both get and set accessors. Note
        // that neither the get or set accessor may have a body.
        abstract public string StringProp
        {
            get;
            set;
        }
        // Declare a method that will access the abstract members.
        public void GetAbstract ()
        {
            // Get the DoubleProp, which will be in the derived class.
            Console.WriteLine ("DoubleProp = " + DoubleProp);
            // You can only set the IntProp value. The storage is in the
            // derived class.
            IntProp = 42;

            // Set the StringProp value
            StringProp = "StringProperty actually is stored in " +
                         "the derived class.";
            // Now show StringProp
            Console.WriteLine (StringProp);
            
            // Finally, call the abstract method
            Describe ();
        }
    }
    // Derive a class from clsBase. You must implement the abstract members
    class clsDerived : clsBase
    {
        // Declare a constructor to set the DoubleProp member
         public clsDerived (double val)
         {
             m_Double = val;
         }
        // When you implement an abstract member in a derived class, you may not
        // change the type or access level.
        override public void Describe()
        {
            Console.WriteLine ("You called Describe() from the base " +
                               "class but the code body is in the \r\n" +
                               "derived class");
            Console.WriteLine ("m_Int = " + m_Int);
        }

        // Implement the DoubleProp property. This is where you provide a body
        // for the accessors.
        override public double DoubleProp
        {
            get {return (m_Double);}
        }
        // Implement the set accessor for IntProp.
        override public int IntProp
        {
            set {m_Int = value;}
        }
        
        // Implement StringProp, providing a body for both the get
        // and set accessors.
        override public string StringProp
        {
            get {return (m_String);}
            set {m_String = value;}
        }
        
        // Declare fields to support the properties.
        private double m_Double;
        private int m_Int;
        private string m_String;
    }
}

           
       

Declare class and use it

Saturday, June 27th, 2009

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System; 
 
class Rect { 
  public int width; 
  public int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 

  
public class UseRect { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
    Rect r2 = new Rect(7, 9); 
 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine("Area of r2: " + r2.area()); 
 
  } 
}

           
       

Properties Accessors

Thursday, June 25th, 2009

/*
A Programmer’s Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 18 - Properties\Accessors
// copyright 2000 Eric Gunnerson

public class PropertiesAccessors
{
    private string name;
    
    public string Name
    {
        get 
        {
            return name;
        }
        set 
        {
            name = value;
        }
    }
}