In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use C# generics collection examples. I hope you will get something after reading this article. Let's discuss it together.
Before we knew about C # generic collections, we understood that collections are an important concept in OOP, and the full support for collections in C # is one of the essence of the language. C # generics are a new element 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. It may be better to think of generics (including classes, interfaces, methods, delegates, etc.) as templates, and the variant parts of the template will be replaced by the class name passed in as parameters, resulting in a new type definition. Generics is a relatively large topic, which will not be analyzed in detail here. Those who are interested can consult the relevant information.
C # generic collection classes are 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 #. Let's take a look at my implementation code, where comments have been written, so I won't explain the code any more:
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 set-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 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.nexttextual 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 public T GetAtIndex (int index) {int innercount = 1; MyListNode first = this.firstNode on the specified index 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 public void InsertAtIndex (int index,T data) {int innercount = 1; MyListNode first = this.firstNode; if (index > count) {throw new Exception ("Index out of boundary") on the specified index } 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 the collection element 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 the collection class can be traversed with foreach public IEnumerator GetEnumerator () {MyListNode first = this.firstNode; while (firstworthy = null) {yield return first.data; first = first.next }} / / element values on the internal node class private class MyListNode {public T data {get; set;} / / 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 mock class by the 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"); ml.InsertAtIndex (4, "fiercely"); ml.RemoveAtIndex (2); ml.Remove ("lin"); foreach (string s in ml) {Console.WriteLine (s) } after reading this article, I believe you have a certain understanding of "how to use C# generic collection examples". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.