Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sequence table of C # collection

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to use the sequence table of the C# collection". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the sequence table of the C# collection" can help you solve your puzzles. the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

If you need to sort the desired collections based on keys, you can use the SortedList class. This class sorts elements by key. Values and keys in this collection can be of any type. Custom types defined as keys need to implement the IComparer interface to sort the elements in the list.

Create an ordered list using the constructor and add it with the Add method:

Var books = new SortedList (); books.Add ("Professional WPF Programming", "978-0470-04180-2"); books.Add ("Professional ASP.NET MVC 3", "978-1-1180-7658-3")

You can also use the indexer to add elements to the list

Books ["Beginning Visual C# 2010"] = "978-0470-50226-6"; books ["Professional C# 4 and .NET 4"] = "978-0470-50225-9"; SortedList has multiple overloaded versions of the constructor.

You can use the foreach statement to iterate through the list, and the element returned by the enumerator is of type KeyValuePair, which contains keys and values:

Foreach (KeyValuePair book in books) {Console.WriteLine ("{0}, {1}", book.Key, book.Value);}

The iterative statement is displayed in the order in which the keys are pressed:

Beginning Visual C # 2010, 978-0470-50226-6 Professional ASP.NET MVC 3978-1-1180-7658-3 Professional C# 4 and .net 4,978-0470-50225-9 Professional WPF Programming, 978-0470-04180-2

You can also use the Values and Keys properties to access values and keys:

Foreach (string isbn in books.Values) {Console.WriteLine (isbn);} foreach (string title in books.Keys) {Console.WriteLine (title);}

If you try to access an element using an indexer, but the passed key does not exist, an exception is thrown. The ContainsKey () method determines whether the passed key exists in the collection. TryGetValue this method attempts to get the value of the specified key and does not throw an exception if the value for the specified = key does not exist.

After reading this, the article "how to use the sequence table of C# collections" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report