1.56. C# sortedlist

发布时间 : 2025-10-25 13:34:31 UTC      

Page Views: 10 views

The SortedList class represents a series of key/value pairs sorted by key,which can be accessed by key and index.

A sorted list is a combination of arrays and hashtable. It contains a listof items that can be accessed using keys or indexes. If you use an index toaccess items, it is a dynamic arrayList, and if you use keys to access items, it is a Hashtable. Items in the collection are always sorted by key value.

1.56.1. Methods and properties of the SortedList class #

The following table lists some common properties of SortedList class:

Attribute

Description

Capacity

Gets or sets the capacity of the SortedList.

Count

Gets the number of elements in the SortedList.

IsFixedSize

Gets a value indicating whether the SortedList has a fixed size.

IsReadOnly

Gets a value indicating whether the SortedList is read-only.

Item

Gets or sets the value related to the key specified in the SortedList.

Keys

Gets the key in SortedList.

Values

Gets the value in SortedList.

The following table lists some common methods of SortedList :

Serial number

Method name & description

1

Public virtual void Add (object key, object value); adds an element with thespecified key and value to SortedList.

2

Public virtual void Clear (); removes all elements from the SortedList.

3

Public virtual bool ContainsKey (object key); determines whether the SortedList contains the specified key.

4

Public virtual bool ContainsValue (object value); determines whether the SortedList contains the specified value.

5

Public virtual object GetByIndex (int index); gets the value at the specified index of the SortedList.

6

Public virtual object GetKey (int index); gets the key at the specified index of the SortedList.

7

Public virtual IList GetKeyList (); gets the key in SortedList.

8

Public virtual IList GetValueList (); gets the value in SortedList.

9

Public virtual int IndexOfKey (object key); returns the index of the specified key in SortedList, starting at zero.

10

Public virtual int IndexOfValue (object value); returns the index where the specified value appears for the first time in SortedList, starting at zero.

11

Public virtual void Remove (object key); removes the element with the specified key from the SortedList.

12

Public virtual void RemoveAt (int index); removes the element at the specified index of the SortedList.

13

Public virtual void TrimToSize (); sets the capacity to the actual number ofelements in the SortedList.

1.56.2. Example #

The following example demonstrates the concept of a sorted list (SortedList):

Example #

using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Zara Ali"); sl.Add("002", "Abida Rehman"); sl.Add("003", "Joe Holzner"); sl.Add("004", "Mausam Benazir Nur"); sl.Add("005", "M. Amlan"); sl.Add("006", "M. Arif"); sl.Add("007", "Ritesh Saikia"); if (sl.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { sl.Add("008", "Nuha Ali"); } // Get a collection of keys ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + sl[k]); } } } } 

When the above code is compiled and executed, it produces the following results:

001: Zara Ali 002: Abida Rehman 003: Joe Holzner 004: Mausam Banazir Nur 005: M. Amlan 006: M. Arif 007: Ritesh Saikia 008: Nuha Ali 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。