public interface ISs { int this[int index] { set; get; } } class TempRecord: ISs { // Array of temperature values private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 61.3F, 65.9F, 62.1F, 59.2F, 57.5F }; public float[] Temps { get { return temps; } set { temps = value; } }
private int []ss = new int[10]; // To enable client code to validate input // when accessing your indexer. public int Length { get { return temps.Length; } } // Indexer of the class. // If index is out of range, the temps array will throw the exception. public float this[int index] { get { return temps[index]; } set { temps[index] = value; } } //indexer of Interface ISs int ISs.this[int index] { set { ss[index] = value; } get { return ss[index]; } } } class Program { static void Main(string[] args) { TempRecord tr = new TempRecord(); //ISs tr = new TempRecord(); tr[0] = 2; tr.Temps[1] = 3; foreach(float f in tr) { Console.WriteLine(tr[0]); } } }
用HashTable存储就好了using namespace Threading; ... HashTable table; for(int i=0;i<100;i++) { table.Add(i,"第i个数"); } ...foreach(int i in table.Keys) { 取第i个数=(int)table[i].Value; }
using System; using System.Collections.Generic; using System.Text;namespace ConsoleApplication1 { class Program { int[] intX = new int[100];
public int this[int index] { get { if (index < 0 || index > intX.Length) throw new Exception("超出索引!"); return this.intX[index]; } set { if (index < 0 || index > intX.Length) throw new Exception("超出索引!"); this.intX[index] = value; } } } }
class Program { int[] intX = new int[100];
public int this[int index] { get { if (index < 0 || index > intX.Length) throw new Exception("超出索引!"); return this.intX[index]; } set { if (index < 0 || index > intX.Length) throw new Exception("超出索引!"); this.intX[index] = value; } } }
public interface ISs
{
int this[int index]
{
set;
get;
}
}
class TempRecord: ISs
{
// Array of temperature values
private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F,
61.3F, 65.9F, 62.1F, 59.2F, 57.5F }; public float[] Temps
{
get { return temps; }
set { temps = value; }
}
private int []ss = new int[10];
// To enable client code to validate input
// when accessing your indexer.
public int Length
{
get { return temps.Length; }
}
// Indexer of the class.
// If index is out of range, the temps array will throw the exception.
public float this[int index]
{
get
{
return temps[index];
} set
{
temps[index] = value;
}
} //indexer of Interface ISs
int ISs.this[int index]
{
set
{
ss[index] = value;
}
get
{
return ss[index];
}
}
} class Program
{
static void Main(string[] args)
{
TempRecord tr = new TempRecord();
//ISs tr = new TempRecord();
tr[0] = 2;
tr.Temps[1] = 3;
foreach(float f in tr)
{
Console.WriteLine(tr[0]);
}
}
}
...
HashTable table;
for(int i=0;i<100;i++)
{
table.Add(i,"第i个数");
}
...foreach(int i in table.Keys)
{
取第i个数=(int)table[i].Value;
}
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
int[] intX = new int[100];
public int this[int index]
{
get
{
if (index < 0 || index > intX.Length)
throw new Exception("超出索引!");
return this.intX[index];
}
set
{
if (index < 0 || index > intX.Length)
throw new Exception("超出索引!");
this.intX[index] = value;
}
}
}
}
{
int[] intX = new int[100];
public int this[int index]
{
get
{
if (index < 0 || index > intX.Length)
throw new Exception("超出索引!");
return this.intX[index];
}
set
{
if (index < 0 || index > intX.Length)
throw new Exception("超出索引!");
this.intX[index] = value;
}
}
}