Home  >  Article  >  Backend Development  >  A brief analysis of the application of C# generic collection instances

A brief analysis of the application of C# generic collection instances

黄舟
黄舟Original
2016-12-21 14:41:221051browse

C# Before we understand generic collections, we understand that collections are an important concept in OOP, and the comprehensive support for collections in C# is one of the essences of the language. C# generics are a new element in C# 2.0 (called templates in C++), mainly used to solve a series of similar problems. This mechanism allows passing a class name as a parameter to a generic type and producing the corresponding object. It may be better to think of generics (including classes, interfaces, methods, delegates, etc.) as templates. The variant part in the template will be replaced by the class name passed in as a parameter, thereby obtaining a new type definition. Generics is a relatively large topic and will not be analyzed in detail here. Those who are interested can consult relevant information.

​ C# generic collection class is very convenient and fast to use. In this essay, I will use a linked list to simulate the behavior of the List﹤T﹥ class in C#. Without further ado, let’s take a look at my implementation code. Comments have been written in the code, so no additional code will be added. Description:

using System.Collections;
class MyList﹤T﹥
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 - get the collection element at the specified index
  public T GetAtIndex(int ​​index)
  {
  int innercount = 1;
    MyListNode first = this.first Node;
if (index ﹥ count)
                                             throw new Exception("         Index out of boundary");                                                                                                  count++;
}
return first.data;
}
}
//Insert a new element at 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 - delete the collection element at 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;
                                                                                                                                       ;
while (first!= null)
                                                                                                                                                                                      . The element value on the 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 this simulation class by C# generic collection:

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"); T ml.insertatIndex (4, "filely");
ml.removetIndex (2);
ml.remove ("lin");
foreach (string s in ml) {
console.writeline (s);
}
}
}

 The basic content of C# generic collection instance application is introduced to you here. I hope it will be helpful for you to understand and learn C# generic collection.

The above is the content of the brief analysis of C# generic collection instance application. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn