C # Use of the class indexer,
The indexer provides a way for classes to be accessed as arrays. In C #, the class indexer is implemented through the attribute of this.
Index. ToString ("D2") converts an index into a string with two character widths.
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 ();
}
}
}
Result:
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"
C language ^ how to use
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
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C language ^ how to use
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
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].