C # Generic collections we understand that collections are an important concept in OOP. The full support for collections in C # is one of the essence of this language. C # generics are new elements in C #2.0 (called templates in C ++) and are mainly used to solve a series of similar problems. This mechanism allows you to pass the class name as a parameter to the generic type and generate the corresponding object. The generic type (including classes, interfaces, methods, and delegation) can be better understood as a template. The variant part of the template will be replaced by the class name passed in as a parameter, to get a new type definition. Generic is a big topic. I will not elaborate on it here. If you are interested, please refer to the relevant materials.
C # Generic collection classes are very convenient to use. In this article, I will use a linked List to simulate the List <T> class behavior in c #. I will not talk much nonsense. Let's look at my Implementation Code. The Code has already written comments, therefore, the Code is not described as follows:
Using System. Collections;
Class MyList <T>
{
PRivate MyListNode firstNode; // The first node.
Private int count; // C # Generic set-node count
Public MyList ()
{
This. firstNode = null;
This. count = 0;
}
// C # Generic set-get the List Length
Public int GetLength ()
{
Return this. count;
}
// Add a node
Public void AddElement (T data)
{
MyListNode first = this. firstNode;
If (first = null)
{
This. firstNode = new MyListNode (data );
This. count ++;
Return;
}
While (first. next! = Null)
{
First = first. next;
}
First. next = new MyListNode (data );
This. count ++;
}
// C # Generic set-delete a node
Public bool Remove (T data)
{
MyListNode first = this. firstNode;
If (first. data. Equals (data ))
{
This. firstNode = first. next;
This. count --;
Return true;
}
While (first. next! = Null)
{
If (first. next. data. Equals (data ))
{
First. next = first. next. next;
This. count --;
Return true;
}
}
Return false;
}
// C # Generic set-get the set element on the specified index
Public T GetAtIndex (int index)
{
Int innercount = 1;
MyListNode first = this. firstNode;
If (index> count)
{
Throw new Exception ("Index out of boundary ");
}
; Else
{
While (innercount <index)
{
First = first. next;
Innercount ++;
}
Return first. data;
}
}
// Insert a new element into the specified index
Public void InsertAtIndex (int index, T data)
{
Int innercount = 1;
MyListNode first = this. firstNode;
If (index> count)
{
Throw new Exception ("Index out of boundary ");
}
If (index = 1)
{
This. firstNode = new MyListNode (data );
This. firstNode. next = first;
}
Else
{
While (innercount <index-1)
{
First = first. next;
Innercount ++;
}
MyListNode newNode = new MyListNode (data );
NewNode. next = first. next;
First. next = newNode;
}
This. count ++;
}
// C # Generic set-delete the set element on the specified index
Public void RemoveAtIndex (int index)
{
Int innercount = 1;
MyListNode first = this. firstNode;
If (index> count)
{
Throw new Exception ("Index out of boundary ");
}
If (index = 1)
{
This. firstNode = first. next;
}
Else
{
While (innercount <index-1)
{
First = first. next;
Innercount ++;
}
First. next = first. next. next;
}
This. count --;
}
// C # Generic set-delete all elements in the Set
Public void RemoveAll ()
{
This. firstNode = null;
This. count = 0;
}
// You can use foreach to traverse the collection class.
Public IEnumerator GetEnumerator ()
{
MyListNode first = this. firstNode;
While (first! = Null)
{
Yield return first. data;
First = first. next;
}
}
// Internal node class
Private class MyListNode
{
Public T data {get; set;} // element value on the node
Public MyListNode next {get; set;} // next node of the node
Public MyListNode (T nodeData)
{
This. data = nodeData;
This. next = null;
}
}
}
The following is the usage of the simulation class in the C # Generic set:
Class Program
{
Static void Main (string [] args)
{
MyList <string> ml = new MyList <string> ();
Ml. AddElement ("xu ");
Ml. AddElement ("jin ");
Ml. AddElement ("lin ");
Ml. AddElement ("love ");
Ml. AddElement ("jasmine ");
Ml. InsertAtIndex (4, "fiercely ");
Ml. RemoveAtIndex (2 );
Ml. Remove ("lin ");
Foreach (string s in ml)
{
Console. WriteLine (s );
}
}
}