A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.
Let us see an example wherein we added 4 key and value pair for SortedList −
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { SortedList s = new SortedList(); s.Add("S1", "Maths"); s.Add("S2", "Science"); s.Add("S3", "English"); s.Add("S4", "Economics"); } } }
让我们现在来看看如何获取SortedList的键并显示它 −
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("ST0", "One"); sl.Add("ST1", "Two"); sl.Add("ST2", "Three"); sl.Add("ST3", "Four"); sl.Add("ST4", "Five"); sl.Add("ST5", "Six"); sl.Add("ST6", "Seven"); ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k); } } } }
以上就是C# 中的 SortedList 类是什么?的详细内容,更多请关注php中文网其它相关文章!
已抢26920个
抢已抢14303个
抢已抢7179个
抢已抢8639个
抢已抢17634个
抢已抢7106个
抢