Analysis of the application of C # generic collection instance

Source: Internet
Author: User
C # Generic Collections before we understand that collections are an important concept in OOP, the full support for collections in C # is one of the best of the language. C # generics are new elements in C # 2.0 (called templates in C + +) and are primarily used to solve a series of similar problems. This mechanism allows you to pass a class masterpiece as a parameter to a generic type and generate the corresponding object. It may be better to understand generics (including classes, interfaces, methods, delegates, etc.) as templates, and the variant parts of the template will be replaced with the class names passed in as parameters, resulting in a new type definition. Generics are a relatively large topic, where no detailed analysis, interested people can access the relevant information.

C # Generic collection classes are very handy and quick to use. In this essay, I will use a linked list to simulate the behavior of the List﹤t﹥ class in C #, not much to say, see my implementation code below, the code has written a comment, so no longer the code for additional instructions:

Using System.Collections;
Class Mylist﹤t﹥
{
PRivate Mylistnode firstnode;//First node
private int count;//c# Generic collection-node count
Public MyList ()
{
This.firstnode = null;
This.count = 0;
}
C # Generic Collection-Get 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 Collection-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 Collection-Gets the collection 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;
}
}
Inserts a new element on 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 Collection-Deletes a collection element on a 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 Collection-Deletes all elements in the collection
public void RemoveAll ()
{
This.firstnode = null;
This.count = 0;
}
To implement this collection class can traverse with foreach
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 values on a node

Public Mylistnode Next {get; set;} The next node of the node
Public Mylistnode (T nodedata)
{
This.data = Nodedata;
This.next = null;
}
}
}

The following is the use of the C # generic collection for this impersonation class:

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);
}
}
}

The basic content of the C # generic collection instance app is introduced to you and hopefully helps you understand and learn about C # generic collections.

The above is the C # generic collection instance application analysis of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.