Trending September 2023 # Functions Of Sortedlist In C# With Examples # Suggested October 2023 # Top 12 Popular | Uyenanhthammy.com

Trending September 2023 # Functions Of Sortedlist In C# With Examples # Suggested October 2023 # Top 12 Popular

You are reading the article Functions Of Sortedlist In C# With Examples updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Functions Of Sortedlist In C# With Examples

Introduction to C# SortedList

Web development, programming languages, Software testing & others

Syntax:

SortedListlist_name = new SortedList();

Where list_name is the name of the list.

Working of SortedList in C#

A collection of pairs of keys and values that are sorted in ascending order by default based on the keys and is identified by keys or indexes is called SortedList in C#.

Two arrays are maintained by the SortedList internally among which one array is used to store the keys and the other array is used to store the values associated with the keys.

A key can never be null whereas the value can be null.

The number of elements held by the SortedList is the capacity of the SortedList.

Duplicate keys are not allowed in the SortedList.

As the elements of the SortedList will be sorted, the operations performed on the SortedList are slower.

An integer index can be used to access the elements in the collection.

Examples of C# SortedList

Below are the examples of SortedList C#:

Example #1

C# program to create a SortedList and to check if the object of the SortedList had a fixed size or no and also to check if the SortedList is read only or not.

Code:

using System; using System.Collections; class program { public static void Main() { SortedList List = new SortedList(); List.Add("10", "Shobha"); List.Add("20", "Ramya"); List.Add("30", "Rahul"); List.Add("40", "Bhuvan"); List.Add("50", "Kiran"); for (int r = 0; r <List.Count; r++) { Console.WriteLine("{0} and {1}", List.GetKey(r), List.GetByIndex(r)); } Console.WriteLine(List.IsFixedSize); Console.WriteLine(List.IsReadOnly); } }

Explanation: In the above program, a class called program is created. Then the main method is called. Then a new sorted list is created. Then elements are added to the newly created sorted list in the form of keys and values pairs. Then the elements of the sorted list are displayed by using the keys. Then by using a property of sorted list, the sorted list is checked if it has a fixed size or no. Then by using a property of sorted list, the sorted list is checked if it is read-only or not. The output of the program is shown in the snapshot above.

Example #2

C# program to create a sorted list and to display the number of elements in the sorted list and to display the capacity of the sorted list and to clear all the elements of the sorted list.

Code:

using System; using System.Collections; class program { public static void Main() { SortedList List = new SortedList(); List.Add("10", "Shobha"); List.Add("20", "Ramya"); List.Add("30", "Rahul"); List.Add("40", "Bhuvan"); List.Add("50", "Kiran"); Console.WriteLine("The count of the elements in the sorted list is : " + List.Count); Console.WriteLine("The newly created sorted list's capacity is : " + List.Capacity); List.Clear(); Console.WriteLine("The count of the elements in the sorted list after using the clear() function is : " + List.Count); Console.WriteLine("The sorted list's capacity after using the clear() function is : " + List.Capacity); } }

Explanation: In the above program, a class called program is created. Then the main method is called. Then a new sorted list is created. Then elements are added to the newly created sorted list in the form of keys and values pairs. Then the count of the elements of the sorted list are displayed by using count property. Then by using the capacity property of sorted list, the sorted list is checked for its capacity. Then by using the clear property of sorted list, the elements in the sorted list are deleted. Then again the count of the elements of the sorted list are displayed by using count property. Then by using the capacity property of sorted list again, the sorted list is checked for its capacity. The output of the program is shown in the snapshot above.

Advantages

SortedList does not allow duplicate keys.

Values of the same types and values of different types can be stored in a SortedList due to non-generic collection.

IEnumerable, Icollection, Iclonable, and Dictionary interfaces are implemented by the SortedList class.

Recommended Article

You're reading Functions Of Sortedlist In C# With Examples

Update the detailed information about Functions Of Sortedlist In C# With Examples on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!