索引器提供了一種可以讓類被當作數組進行訪問的方式。在C#中,類索引器是通過this的屬性實現的。
index.ToString("D2")將index轉換成一個具有兩個字符寬度的字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassIndexer
{
/// <summary>
/// define a class called AClass,which has only one private variable
/// </summary>
class AClass {
private string _name;
public string Name {
get { return this._name; }
set { this._name = value; }
}
public AClass(string name) {
this._name = name;
}
}
/// <summary>
/// AClassContainer class
/// </summary>
class AClassContainer {
private AClass[] _AClassList;
public AClassContainer() {
this._AClassList = new AClass[10];
}
public AClassContainer(int cap) {
this._AClassList = new AClass[cap];
}
/// <summary>
/// class indexer by using int
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public AClass this[int index] {
get {
if ((index < 0) || (index > this._AClassList.Length)) return null;
else return this._AClassList[index];
}
set {
if ((index < 0) || (index > this._AClassList.Length)) return;
else this._AClassList[index] = value;
}
}
/// <summary>
/// class indexer by using string
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public AClass this[string name] {
get {
foreach (AClass cls in this._AClassList) {
if (cls.Name == name) return cls;
}
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
int count = 10;
AClassContainer classContainer = new AClassContainer(count);
for (int index = 0; index < count; index++) {
string name = index.ToString("D2");
classContainer[index] = new AClass(name);
}
for (int index = 0; index < count; index++)
{
AClass cls = classContainer[index];
System.Console.WriteLine("classContainer[{0}].Name=\"{1}\"",index,cls.Name);
}
System.Console.WriteLine();
for (int index = count - 1; index >= 0; index--) {
string name = index.ToString("D2");
AClass cls = classContainer[name];
System.Console.WriteLine("classContainer[{0}].Name=\"{1}\"",name,cls.Name);
}
System.Console.ReadLine();
}
}
}
結果:
classContainer[0].Name="00"
classContainer[1].Name="01"
classContainer[2].Name="02"
classContainer[3].Name="03"
classContainer[4].Name="04"
classContainer[5].Name="05"
classContainer[6].Name="06"
classContainer[7].Name="07"
classContainer[8].Name="08"
classContainer[9].Name="09"
classContainer[09].Name="09"
classContainer[08].Name="08"
classContainer[07].Name="07"
classContainer[06].Name="06"
classContainer[05].Name="05"
classContainer[04].Name="04"
classContainer[03].Name="03"
classContainer[02].Name="02"
classContainer[01].Name="01"
classContainer[00].Name="00"
a1 = 0x01; //0000 0001
a2 = 0x00; //0000 0000
a3 = 0x03; //0000 0011
a4 = 0x02; //0000 0010
b1 = a1 ^ a2; //0000 0001
b2 = a1 ^ a3; //0000 0010
b3 = a1 ^ a4; //0000 0011
^異或運算符,位值相同為0,不同為1,見上示例.
//
簡單實際問題舉例:
======\=======\=======
======a=======b=======
上面是2條電路,2個開關分別為a和b,打開狀態:\[1],關閉狀態:/[0].
若同時打開或者關閉,兩條電路均不通.
若a打開[1],b關閉[0],電路1通電
======\=======/=======
若a關閉[0],b打開[1],電路2通電
======/=======\=======
綜上,電路在a,b狀態相同時不通[0],在a,b不同時通電[1].
a1 = 0x01; //0000 0001
a2 = 0x00; //0000 0000
a3 = 0x03; //0000 0011
a4 = 0x02; //0000 0010
b1 = a1 ^ a2; //0000 0001
b2 = a1 ^ a3; //0000 0010
b3 = a1 ^ a4; //0000 0011
^異或運算符,位值相同為0,不同為1,見上示例.
//
簡單實際問題舉例:
======\=======\=======
======a=======b=======
上面是2條電路,2個開關分別為a和b,打開狀態:\[1],關閉狀態:/[0].
若同時打開或者關閉,兩條電路均不通.
若a打開[1],b關閉[0],電路1通電
======\=======/=======
若a關閉[0],b打開[1],電路2通電
======/=======\=======
綜上,電路在a,b狀態相同時不通[0],在a,b不同時通電[1].