1. Student Data category
public class Student
{
Public Student ()
{ }
Public Student (string no,string name)
{
This. Studentno = no;
This. Studentname = name;
}
private string Studentno;
private string Studentname;
<summary>
School Number
</summary>
public string Studentno
{
Get
{
return studentno;
}
Set
{
Studentno = value;
}
}
<summary>
name
</summary>
public string Studentname
{
Get
{
return studentname;
}
Set
{
Studentname = value;
}
}
}
2. Arrays
public class MyList
{
<summary>
The actual number of array elements
</summary>
private int count;
student[] Stus = new Student[4];
Number of #region elements
public int Count
{
Get
{
return count;
}
Set
{
Count = value;
}
}
#endregion
#region adding elements
public void Add (Student item)
{
if (Count = = Stus. Length)
{
student[] Newstus = new Student[stus. Length * 2];
Stus. CopyTo (newstus, 0);
Stus = Newstus;
}
Stus[count] = Item;
count++;
}
#endregion
#region Get elements
Public Student Get (int index)
{
return Stus[index];
}
Public Student Get (string name)
{
for (int i = 0; i < count; i++)
{
if (Stus[i]. Studentname = = name)
{
return stus[i];
}
}
return null;
}
#endregion
#region Indexer
Public Student This[int Index]
{
Get
{
return Stus[index];
}
set {Stus[index] = value;}
}
Public Student this[string Name]
{
Get
{
for (int i = 0; i < count; i++)
{
if (Stus[i]. Studentname = = name)
{
return stus[i];
}
}
return null;
}
Set
{
for (int i = 0; i < count; i++)
{
if (Stus[i]. Studentname = = name)
{
Stus[i] = value;
}
}
}
}
#endregion
}
3. Call
static void Main (string[] args)
{
MyList ml = new C0index. MyList ();
ml. ADD (New Student ("001", "AA"));
ml. ADD (New Student ("002", "BB"));
ml. ADD (New Student ("003", "CC"));
ml. ADD (New Student ("004", "DD"));
ml. ADD (New Student ("005", "ee"));
for (int i = 0; i < ml. Count; i++)
{
Console.WriteLine (Ml[i]. Studentname);
}
}
C # Indexer Encapsulation