Loading ...

SortedSet<T> Collection in .NET 4.0 | CodeAsp.Net

SortedSet<T> Collection in .NET 4.0

 /5
0 (0votes)

In .NET Framework 4.0, we can sort a list of elements without explicitly implementing any sort algorithms through a new class SortedSet<T>.

Let’s create an instance of SortedSet class as-

SortedSet<string> sortedSet = new SortedSet<string> { "samir", "kumar", "nigam", "tanu", "nitin" };

 

Let’s print this set through foreach loop as-

foreach (string element in sortedSet)
            Response.Write(string.Format("  {0}", element));

 

And we get sorted output as-

kumar  nigam  niti  samir  tanu
 
You can also Add elements in the set at run time and set will get sorted implicitly. e.g. let’s add an element in the set as-

sortedSet.Add("ram");

foreach (string element in sortedSet)
            Response.Write(string.Format("  {0}", element));

 

And we get sorted output as-

kumar  nigam  niti  ram  samir  tanu

Really this is very great new feature in .NET 4.0

Comments (no comments yet)

Top Posts