C# SortedList Tutorial with Examples | Learn in 1 Day FREE
C Sorted List Tutorial ACTE

C# Sorted List Tutorial with Examples | Learn in 1 Day FREE

Last updated on 02nd Feb 2022, Blog, Tutorials

About author

Manobala (C# Automation Tester )

Manobala is a C# automation tester and he has tons of experience in the areas of HTTP, SOAP/REST, VisualStudio, TFS, etc., CI/CD, functional, regression, and.NET technology stack. He spends most of his time researching technology and startups.

(5.0) | 19348 Ratings 1885
    • Introduction to C# Sorted List
    • How arranged rundown will add the qualities in C#?
    • Sorted List Characteristics
    • Eliminate Elements from Sorted List
    • Significant Points
    • How to make a Sorted List?
    • Constructors of Sorted List C#
    • Features of sorted list
    • Properties of sorted list
    • Sorted List Properties
    • Making a Sorted List
    • Methods of sorted list
    • Working of Sorted List in C#
    • Advantages
    • Prerequisites
    • Conclusion

    Subscribe For Free Demo


      Introduction to C# Sorted List:

      C# Sorted List<''> class addresses an assortment of key/esteem matches that are arranged by the key. C# Sorted List is a conventional assortment, characterized as Sorted List<'TKey, TValue>, where Turkey is the kind of key in the assortment and Tvalue is the sort of qualities in the assortment. In this article, I disclose how to make a Sorted List item and use Sorted List techniques and Sorted List properties on the assortment.

      How arranged rundown will add the qualities in C#?

      The Sorted List<'int, string> will store keys of int type and benefits of string type. The Add() strategy is utilized to add a solitary key-esteem pair in a Sorted List. Keys can’t be invalid or copied. Whenever found, it will toss a run-time exemption.

      C# Sorted List:

      In C#, Sorted List is an assortment of key/esteem sets that are arranged by keys. Of course, this assortment sort the key/esteem sets in rising request. It is of both nonexclusive and non-conventional sort of assortment. The conventional Sorted List is characterized in System.Collections.Generic namespace while non-nonexclusive Sorted List is characterized under System. Collections namespace, here we will examine non-nonexclusive sort Sorted List.

      Course Curriculum

      Learn Advanced C and C Plus Certification Training Course to Build Your Skills

      Weekday / Weekend BatchesSee Batch Details

      C# – Sorted List<'TKey, TValue>:

    • The Sorted List<'TKey, TValue>, and Sorted List are assortment classes that can store key-esteem matches that are arranged by the keys given the related IComparer execution. For instance, on the off chance that the keys are of crude kinds, arranged in the climbing request of keys.

    • C# upholds conventional and non-nonexclusive Sorted List. It is prescribed to utilize nonexclusive Sorted List<'TKey, TValue> because it performs quicker and less blunder inclined than the non-conventional Sorted List.

    • Sorted List Characteristics:

    • Sorted List<'TKey, TValue> is a variety of key-esteem sets arranged by keys.
    • Sorts components when they are added. Sorts crude sort keys in climbing request and article keys in light of IComparer<'T>.
    • Goes under System.Collection.Generic namespace.
    • A critical should be novel and can’t be invalid.
    • A worth can be invalid or copied.
    • A worth can be gotten to bypassing related key in the indexer mySorted List[key]
    • Contains components of type KeyValuePair<'TKey, TValue>
    • It utilizes less memory than SortedDictionary<'TKey, TValue>.
    • It is quicker in the recovery of information once arranged, while SortedDictionary<'TKey, TValue> is quicker in addition and eliminating key-esteem sets.

    • Eliminate Elements from Sorted List:

      Utilize the Remove(key) and RemoveAt(index) strategies to eliminate key-esteem sets from a Sorted List.

      Model: Remove Elements:

      • “Sorted List<'int,string> numberNames = new Sorted List<'int,string>()
      • {
      • {3, “Three”},
      • {1, “One”},
      • {2, “Two”}
      • ,
      • {5, “Five”},
      • {4, “Four”}
      • };
      • numberNames.Remove(1);//eliminates key 1 sets
      • numberNames.Remove(10);//eliminates key 1 sets, no blunder on the off chance that not exists
      • numberNames.RemoveAt(0);//eliminates key-esteem pair from list 0
      • //numberNames.RemoveAt(10);//run-time exemption: ArgumentOutOfRangeException
      • foreach(var kvp in numberNames)
      • Console.WriteLine(“key: {0}, esteem: {1}”, kvp.Key , kvp.Value );”

      Significant Points:

    • The Sorted List class executes the IEnumerable, ICollection, IDictionary, and ICloneable connection points.
    • In Sorted List, a component can be gotten to by its critical or by its record.
    • A Sorted List object inside keeps two clusters to store the components of the rundown, i.e, one exhibit for the keys and one more exhibit for the related qualities.
    • Here, a key can’t be invalid, however, the worth can be.
    • The limit of a Sorted List object is the quantity of key/esteem sets it can host
    • In Sorted List, copy keys are not permitted.
    • In Sorted List, you can store upsides of a similar kind and the various sorts because of the non-nonexclusive assortment. On the off chance that you utilize a conventional Sorted List in your program, it is fundamental that the kind of the qualities should be something similar.
    • In Sorted List you can’t store keys of various information types in a similar Sorted List because the compiler will toss a special case. Thus, consistently add the key in your Sorted List of a similar kind.
    • You can likewise project key/esteem pair of Sorted List into DictionaryEntry.

    • How to make a Sorted List?

      The Sorted List class gives 6 unique kinds of constructors which are utilized to make a Sorted List, here we just use Sorted List(), constructor. To peruse more with regards to Sorted List’s constructors you can allude to C# | Sorted List Class.

      Sorted List():

      It is utilized to make an occasion of the Sorted List class that is vacant, has the default starting limit, and is arranged by the IComparable connection point carried out by each key added to the Sorted List object.

      Stage 1:

      Include System.Collections namespace in your program with the assistance of utilizing watchword: utilizing System. Collections;

      Stage 2:

      Create a Sorted List utilizing Sorted List class as displayed beneath:

      • Sorted List list_name = new Sorted List();

      Stage 3:

      If you need to add a key/esteem pair in your Sorted List, then, at that point, use Add() strategy to add key/esteem sets in your Sorted List. What’s more, you can likewise store a key/esteem pair in your Sorted List without utilizing the Add() technique, this kind of linguistic structure is referred to as Collection Initializer Syntax as displayed in the underneath model.

      Stage 4:

      The key/esteem sets of the Sorted List is gotten to utilizing three unique ways:

      for circle:

      You can use for the circle to get to the key/esteem sets of the Sorted List.


      • CSharp
      • for (int x = 0; x < my_slist1.Count; x++)
      • {
      • Console.WriteLine(“{0} and {1}”,
      • my_slist1.GetKey(x),
      • my_slist1.GetByIndex(x));

      • }

      Utilizing Index:

      You can get to the singular worth of the Sorted List by utilizing the list. You want to pass the key or record as a boundary to track down the particular worth. If the predetermined key isn’t accessible, then, at that point, the compiler will toss a blunder.

      Constructors of Sorted List C#:

      Features of sorted list:

    • A Sorted List object inside keeps two clusters to store the components of the rundown; that is, one exhibit for the keys and one more exhibit for the related qualities. Every component is a key/esteem pair that can be gotten to as a DictionaryEntry object. A key can’t be invalid, yet a worth can be.

    • The limit of a Sorted List object is the number of components the Sorted List can hold. As components are added to a Sorted List, the limit is naturally expanded as expected through redistribution. The limit can be diminished by calling TrimToSize or by setting the Capacity property expressly.

    • .NET Framework as it were: For exceptionally enormous Sorted List objects, you can expand the greatest ability to 2 billion components on a 64-bit framework by setting the empowered quality of the <'gcAllowVeryLargeObjects> setup component to be valid in the run-time climate.

    • The components of a Sorted List object are arranged by the keys either as per a particular IComparer execution indicated when the Sorted List is made or as per the IComparable execution given by the actual keys. Regardless, a Sorted List doesn’t permit copy keys.

    • The record arrangement depends on the sort grouping. At the point when a component is added, it is embedded into Sorted List in the right sort request and the ordering changes in like manner. Whenever a component is taken out, the ordering additionally changes in like manner. Thusly, the file of a particular key/esteem pair may change as components are added or eliminated from the Sorted List object.

    • Procedure on a Sorted List object will quite often be slower than procedure on a Hashtable item in light of the arranging. Be that as it may, the Sorted List offers greater adaptability by permitting admittance to the qualities either through the related keys or through the records.

    • Components in this assortment can be gotten to utilizing a number record. Lists in this assortment are zero-based.

    • Properties of sorted list:

      Course Curriculum

      Get JOB Oriented C and C Plus Training for Beginners By MNC Experts

      • Instructor-led Sessions
      • Real-life Case Studies
      • Assignments
      Explore Curriculum

      Sorted List Properties:

    • Sorted List.Capacity
    • Sorted List.Count
    • Sorted List.Keys
    • Sorted List.Values
    • Sorted List.Methods
    • Sorted List.ContainsKey()
    • Sorted List.ContainsValue()
    • Sorted List.Clear()
    • Sorted List.IndexOfKey()
    • Sorted List.IndexOfValue()
    • Sorted List.Remove()

    • Making a Sorted List:

      The accompanying model shows how to make a nonexclusive Sorted List<'TKey, TValue>, and add key-esteem sets in it.

      Model: Create a Sorted List and Add Elements:

      • “//Sorted List of int keys, string esteems
      • Sorted List<'int, string> numberNames = new Sorted List<'int, string>();
      • numberNames.Add(3, “Three”);
      • numberNames.Add(1, “One”);
      • numberNames.Add(2, “Two”);
      • numberNames.Add(4, invalid);
      • numberNames.Add(10, “Ten”);
      • numberNames.Add(5, “Five”);
      • //The accompanying will toss special cases
      • //numberNames.Add(“Three”, 3);//Compile-time blunder: key should be int type
      • //numberNames.Add(1, “One”);//Run-time exemption: copy key
      • //numberNames.Add(null, “Five”);//Run-time exemption: key can’t be invalid

      Getting to Sorted List

      Indicate a key in the indexer Sorted List[key], to get or set worth in the Sorted List.

      Model: Access Sorted List Values

      • Sorted List<'int,string> numberNames = new Sorted List<'int,string>()
      • {
      • {3, “Three”},
      • {1, “One”},
      • {2, “Two”}
      • };
      • Console.WriteLine(numberNames[1]);//yield: One
      • Console.WriteLine(numberNames[2]);//yield: Two
      • Console.WriteLine(numberNames[3]);//yield: Three
      • //Console.WriteLine(numberNames[10]);//run-time KeyNotFoundException
      • numberNames[2] = “TWO”;//refreshes esteem
      • numberNames[4] = “Four”;//adds another key-esteem on the off chance that a key doesn’t exists”

      Methods of sorted list:

      Working of Sorted List in C#:

    • An assortment of sets of keys and qualities that are arranged in climbing requests naturally founded on the keys and is recognized by keys or files is called Sorted List in C#.
    • Two exhibits are kept up with by the Sorted List inside among which one cluster is utilized to store the keys and the other cluster is utilized to store the qualities related to the keys.
    • A key can never be invalid while the worth can be invalid.
    • The quantity of components held by the Sorted List is the limit of the Sorted List.
    • Copy keys are not permitted in the Sorted List.
    • As the components of the Sorted List will be arranged, the tasks performed on the Sorted List are slower.
    • A number list can be utilized to get to the components in the assortment.

    • Advantages:

      There are a few benefits of involving Sorted List in C#. They are:

    • Sorted List doesn’t permit copy keys.
    • Upsides of similar kinds and upsides of various kinds can be put away in a Sorted List because of the non-nonexclusive assortment.
    • The key and worth sets of the Sorted List can be projected into Dictionary passage.
    • IEnumerable, Icollection, Iclonable, and Dictionary points of interaction are executed by the Sorted List class.

    • Prerequisites:

      The instructional exercise expects that you have a machine set up for neighborhood improvement. On Windows, Linux, or macOS, you can utilize the .NET CLI to make, fabricate, and run applications. On Mac and Windows, you can utilize Visual Studio 2019. For arrangement directions, see Set up your nearby climate.

      C and C Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download


      In this instructional exercise, we comprehend the idea of Sorted List in C# through definition, the grammar of Sorted List in C#, working of Sorted List in C# through models and their results, and benefits of involving Sorted List in C#.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free