C # Custom Collections
For the vast majority of game developers who are based on the Unity game engine to develop business-grade high-quality games, it is necessary to develop our "custom collections" using the C # language to develop technology applications such as "Object buffer pooling".
According to the author's experience, a good C # "custom collection" needs to meet the following requirements:
1: You can use foreach to iterate through the collection elements conveniently.
2: Use indexer technology to provide a direct way to access or assign internal elements.
3: Provides common access methods similar to the IList interface:
ADD (), Clear (), Insert (), Remove ()
This technique requires the reader to have a collection, indexer and other relevant knowledge in advance (readers can see the author's previous blog Introduction article), in order to illustrate, first give 4 demonstration classes, a C # custom collection to form a demo example.
First: Entity class
public class Person
{
public static int i;
private string _name;
private int _age;
public string Name
{
get {return _name;}
set {_name = value;}
}
public int Age
{
get {return _age;}
set {_age = value;}
}
Public person ()
//{
i++;
Name = "Zhang San" + i.tostring ();
//}
Public person (string Strname,int intAge)
{
_name = StrName;
_age = IntAge;
}
}
Second: Collection Core class
public class Personcollection:ienumerable//indicates that the enumerable collection interface can be traversed
{
Holds an array of type object.
ArrayList al = new ArrayList ();
<summary>
After implementing the Ienumberable, an iterator object is returned, but the specific interface subclass is determined by the programmer
Define yourself.
</summary>
<returns></returns>
Public IEnumerator GetEnumerator ()
{
return new Mygetenumberater (this.al);
}
<summary>
Indexer
</summary>
<param name= "Index" ></param>
<returns></returns>
Public Person This[int Index]
{
get {return (person) Al[index];}
}
public void Add (person p)
{
Al. ADD (P);
}
public void AddRange (ICollection IP)
{
Al. AddRange (IP);
}
public void Clear ()
{
Al. Clear ();
}
public void Insert (int index,person value)
{
Al. Insert (Index,value);
}
public int IndexOf (person p)
{
Return al. IndexOf (P);
}
public void RemoveAt (int index)
{
Al. RemoveAt (index);
}
public void Remove (person p)
{
if (al. Contains (P))
{
int temp = INDEXOF (p);
RemoveAt (temp);
}
}
}//class_end
3rd: A custom iterator class
public class Mygetenumberater:ienumerator
{
int i = 0;
ArrayList PS;
Public Mygetenumberater (ArrayList p)
{
PS = p;
}
<summary>
Get current element
</summary>
public Object Current
{
get {return ps[i++];} Note here that the i++, is the first operation and then self-increment.
Equivalent
Object O=ps[i];
i++;
return o;
}
<summary>
Move to the next element
</summary>
<returns></returns>
public bool MoveNext ()
{
if (i > Ps. COUNT-1)
{
return false;
}
return true;
}
Reset
public void Reset ()
{
i = 0;
}
}
4th: Test class for testing "custom collections"
Class Program
{
static void Main (string[] args)
{
Personcollection pc = new Personcollection ();
Pc. ADD (New person ("Zhang San", 10));
Pc. ADD (New person ("John Doe", 20));
Pc. ADD (New person ("Harry", 25));
The intrinsic foreach is a method in the calling interface
foreach (person item in PC)
{
Console.WriteLine (item. Name+ "" +item. Age);
}
Access using a custom indexer.
Console.WriteLine ("-----use indexer to access------");
Console.WriteLine (Pc[0]. Name);
Console.WriteLine (Pc[0]. Age);
Console.WriteLine (Pc[1]. Name);
Console.WriteLine (Pc[1]. Age);
Console.WriteLine (Pc[2]. Name);
Console.WriteLine (Pc[2]. Age);
Console.ReadLine ();
}
}
? Readers can create test cases by copying the above code. Through the Program class test we found that our own definition of the PersonCollection.cs class can be similar to the ArrayList collection, using foreach as an iterative output, using an "indexer" to directly access the data in the collection.
Our custom "iterator" Class (Mygetenumberater) is actually the internal principle of foreach:
? foreach (person p in PC) {}
? equivalent to the following:
? Ienumberable eab=pc as IEnumerable; An iterative interface
? Ienumberator Etor=eab. GetEnumerator (); Iterative interfaces
? while (Etor. MoveNext ())
? {
? Person P=etor. Current;
?//........
?}?
OK, this section on the C # "custom Collection" knowledge point, the introduction here, if you have a problem, you can always leave a message to discuss. Thank you!
C # Custom Collections