Demonstrate the ByTwos 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 ByTwos() {
start = 0;
val = 0;
}

public int getNext() {
val += 2;
return val;
}

public void reset() {
val = start;
}

public void setStart(int x) {
start = x;
val = start;
}
}

// Demonstrate the ByTwos interface.

public class SeriesDemo {
public static void Main() {
ByTwos ob = new ByTwos();

for(int i=0; i < 5; i++) Console.WriteLine("Next value is " + ob.getNext()); Console.WriteLine(" Resetting"); ob.reset(); for(int i=0; i < 5; i++) Console.WriteLine("Next value is " + ob.getNext()); Console.WriteLine(" Starting at 100"); ob.setStart(100); for(int i=0; i < 5; i++) Console.WriteLine("Next value is " + ob.getNext()); } } [/csharp]

Interface with method name conflicts

   
 
using System;

public delegate void AlarmEvent(IAlarm sender);

public interface IAlarm {
    event AlarmEvent IssueAlarm;
}

abstract class MyStuff : ICloneable {
    public object Clone() {
        return null;
    }
    public void DoStuff() {
    }

}
interface IFoo {
    void DoStuff();
}

interface IBar {
    void DoStuff();
}

interface ITest {
    void DoSomething();
    int DoSomethingElse();
}
class MyClass : IFoo, IBar {
    void IFoo.DoStuff() {
    }
    void IBar.DoStuff() {
    }
}

class MainClass : IComparable {
    public int CompareTo(object other) {
        return -1;
    }
    static int Main(string[] args) {
        MainClass c = new MainClass();
        MainClass c2 = new MainClass();
        if (c.CompareTo(c2) == 0)
            return 0;
        MyClass c3 = new MyClass();
        return 1;
    }
}

    


Return class object from indexer

   
 
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
        }
    }
}

    


Indexing with Multiple Parameters


   

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

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 19 - Indexers and EnumeratorsIndexing with Multiple Parameters
// copyright 2000 Eric Gunnerson
using System;

class Player
{
    string name;
    
    public Player(string name)
    {
        this.name = name;
    }
    
    public override string ToString()
    {
        return(name);
    }
}

class Board
{
    Player[,] board = new Player[8, 8];
    
    int RowToIndex(string row)
    {
        string temp = row.ToUpper();
        return((int) temp[0] - (int) &#039;A&#039;);
    }
    
    int PositionToColumn(string pos)
    {
        return(pos[1] - &#039;0&#039; - 1);
    }
    
    public Player this[string row, int column]
    {
        get
        {
            return(board[RowToIndex(row), column - 1]);
        }
        set
        {
            board[RowToIndex(row), column - 1] = value;
        }
    }    
    
    public Player this[string position]
    {
        get
        {
            return(board[RowToIndex(position),
            PositionToColumn(position)]);
        }
        set
        {
            board[RowToIndex(position),
            PositionToColumn(position)] = value;
        }
    }    
}

public class IndexingwithMultipleParameters
{
    public static void Main()
    {
        Board board = new Board();
        
        board["A", 4] = new Player("White King");
        board["H", 4] = new Player("Black King");
        
        Console.WriteLine("A4 = {0}", board["A", 4]);
        Console.WriteLine("H4 = {0}", board["H4"]);
    }
}

           
          


Indexing with an String Index

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

Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/

// 19 – Indexers and EnumeratorsIndexing with an String Index
// copyright 2000 Eric Gunnerson
using System;
using System.Collections;
class DataValue
{
public DataValue(string name, object data)
{
this.name = name;
this.data = data;
}
public string Name
{
get
{
return(name);
}
set
{
name = value;
}
}
public object Data
{
get
{
return(data);
}
set
{
data = value;
}
}
string name;
object data;
}
class DataRow
{
public DataRow()
{
row = new ArrayList();
}

public void Load()
{
/* load code here */
row.Add(new DataValue(“Id”, 5551212));
row.Add(new DataValue(“Name”, “Fred”));
row.Add(new DataValue(“Salary”, 2355.23m));
}

public DataValue this[int column]
{
get
{
return( (DataValue) row[column – 1]);
}
set
{
row[column – 1] = value;
}
}
int FindColumn(string name)
{
for (int index = 0; index < row.Count; index++) { DataValue dataValue = (DataValue) row[index]; if (dataValue.Name == name) return(index); } return(-1); } public DataValue this[string name] { get { return( (DataValue) this[FindColumn(name)]); } set { this[FindColumn(name)] = value; } } ArrayList row; } public class IndexingwithanStringIndex { public static void Main() { DataRow row = new DataRow(); row.Load(); DataValue val = row["Id"]; Console.WriteLine("Id: {0}", val.Data); Console.WriteLine("Salary: {0}", row["Salary"].Data); row["Name"].Data = "Barney"; // set the name Console.WriteLine("Name: {0}", row["Name"].Data); } } [/csharp]

Indexing with an Integer Index


   

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

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 19 - Indexers and EnumeratorsIndexing with an Integer Index
// copyright 2000 Eric Gunnerson
using System;
using System.Collections;
class DataValue
{
    public DataValue(string name, object data)
    {
        this.name = name;
        this.data = data;
    }
    public string Name
    {
        get
        {
            return(name);
        }
        set
        {
            name = value;
        }
    }
    public object Data
    {
        get
        {
            return(data);
        }
        set
        {
            data = value;
        }
    }
    string    name;
    object data;
}
class DataRow
{
    public DataRow()
    {
        row = new ArrayList();
    }
    
    public void Load() 
    {
        /* load code here */ 
        row.Add(new DataValue("Id", 5551212));
        row.Add(new DataValue("Name", "Fred"));
        row.Add(new DataValue("Salary", 2355.23m));
    }
    
    // the indexer
    public DataValue this[int column]
    {
        get
        {
            return((DataValue) row[column - 1]);
        }
        set
        {
            row[column - 1] = value;
        }
    }
    ArrayList    row;    
}
public class IndexingwithanIntegerIndex
{
    public static void Main()
    {
        DataRow row = new DataRow();
        row.Load();
        Console.WriteLine("Column 0: {0}", row[1].Data);
        row[1].Data = 12;    // set the ID
    }
}