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'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) 'A');
}
int PositionToColumn(string pos)
{
return(pos[1] - '0' - 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'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
}
}
C# Properties and Indexers
using System;
public PropertiesIndexers
{
public static void Main()
{
Circle c = new Circle();
c.Radius = 35;
}
}
class Circle
{
public int Radius
{
get
{
return(radius);
}
set
{
radius = value;
Draw();
}
}
public void Draw()
{
}
int radius;
}
Implements an indexer and demonstrates that an indexer does not have to operate on an array
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Seedr.cs -- Implements an indexer and demonstrates that an indexer
// does not have to operate on an array
//
// Compile this program with the following command line:
// C:>csc Seed.cs
//
namespace nsIndexer
{
using System;
public class Seedr
{
static public void Main ()
{
clsIndexed idx = new clsIndexed ();
Console.WriteLine ("The value is " + idx[900]);
}
}
class clsIndexed
{
public int this[int index]
{
get
{
DateTime now = DateTime.Now;
Random rand = new Random ((int) now.Millisecond);
return (rand.Next () % index);
}
}
}
}

