With the latest release of the .NET Framework version 4.0, a new collection has been added to the class library in System.Collections.Generic - SortedSet <T>.
The two main features of SortedSet <T> are:
- Repeat items not allowed
- maintained sorted order when elements are inserted or deleted without affecting performance.
')
To make it clearer, let's pass the tests that I did.
Test 1 : Creating a SortedSet and Printing All Its Elements (standard behavior demonstration)
var sortedSet1 = new SortedSet< int > { 5, 9, 11, 1, 44, 21, 3, 2, 9};
foreach ( int element in sortedSet1)
{
lbl_Test1.Text += " " + element;
}
* This source code was highlighted with Source Code Highlighter .
Result : 1 2 3 5 9 11 21 44
Here we can notice the first feature mentioned above. The number 9 appears twice in the SortedSet, but returns only once as a result.
Test 2 : See the numbers in the range of elements.
var sortedSet1 = new SortedSet< int > { 5, 9, 11, 1, 44, 21, 3, 2, 9 };
foreach ( int element in sortedSet1.GetViewBetween(5, 20))
{
lbl_Test2.Text += " " + element;
}
* This source code was highlighted with Source Code Highlighter .
Result : 5 9 11
In test 2, you can see that this is a fairly useful method that can be used to find all the numbers between the given lower and upper values. In our test, 5 is the lower, and 20 is the upper value.
Test 3 : Difference, Union, and Intersection Operations in SortedSet
var sortedSet1 = new SortedSet< int > { 5, 9, 11, 1, 44, 21, 3, 2, 9 };
var sortedSet2 = new SortedSet< int > { 11, 7, 22, 21, 25, 30};
lbl_Test3.Text += ":" ;
foreach ( int element in sortedSet1.Except(sortedSet2))
{
lbl_Test3.Text += " " + element;
}
lbl_Test3.Text += "; :" ;
foreach ( int element in sortedSet1.Union(sortedSet2))
{
lbl_Test3.Text += " " + element;
}
lbl_Test3.Text += "; :" ;
foreach ( int element in sortedSet1.Intersect(sortedSet2))
{
lbl_Test3.Text += " " + element;
}
* This source code was highlighted with Source Code Highlighter .
Result : Difference: 1 2 3 5 9 44; Consolidation: 1 2 3 5 9 11 21 44 7 22 25 30; Crossing: 11 21;
In test 3 we have two SortedSet. Using the above methods, we perform various operations.
There are some more useful methods, such as
sortedSet1.UnionWith (sortedSet2);
which will permanently merge two SortedSet, excluding duplicate elements.
Test 4 : Removing elements using Remove (int) and RemoveWhere (Predicate)
var sortedSet1 = new SortedSet< int > { 5, 9, 11, 1, 44, 21, 3, 2, 9 };
sortedSet1.Remove(5);
lbl_Test4.Text = " 5:" ;
foreach ( int element in sortedSet1)
{
lbl_Test4.Text += " " + element;
}
sortedSet1.RemoveWhere(X => X % 2 == 0);
lbl_Test4.Text += "; :" ;
foreach ( int element in sortedSet1)
{
lbl_Test4.Text += " " + element;
}
* This source code was highlighted with Source Code Highlighter .
Result : Without element number 5: 1 2 3 9 11 21 44; Only odd items: 1 3 9 11 21.
Note: all results are shown in the <asp: Label /> control with the following IDs: lbl_Test1, lbl_Test2, lbl_Test3, lbl_Test4 - created for testing purposes only, not tied to the entire subject of the blog post.
You can download the working source:
DOWNLOAD .
Hope this post was helpful and informative.