• A2ZMenu.Com
Loading
  • Home
  • Blogs
    • AJAX
    • ASP.NET
    • BI
    • CSharp
    • JavaScript
    • LINQ
    • Miscellaneous
    • SQL
    • SharePoint
    • Silverlight
    • WCF
  • Tutorials
    • HTML5
    • ASP.NET 4.0
    • CSharp
    • SharePoint
    • SQL Function
  • Online Exam
    • ASP.NET
    • LINQ
    • Silverlight
    • SQL
    • JavaScript
    • SharePoint
  • Forum
  • Utility
  • Fun @ Work
    • Appraisal
    • Funny Images
    • Funny Puzzle
    • Miscellaneous
    • Motivating Stories
  • Wiki
  • Contact Us
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • JavaScript
  • LINQ
  • Miscellaneous
  • SQL
  • SharePoint
  • Silverlight
  • WCF

Test Your Skill

Show/Hide
 Q. Out of Boxing and Unboxing which one is implicit?
View all questions...
SortedSet Collection Class in C# 4.0
Tweet
mail-image

Hello Folks,

In the process of exploring new features added to C# 4.0 Class Library, I will discuss about new SortedSet<T> collection. SortedSet<T> is new collection added under the System.Collection.Generic.

SortedSet<T> maintains the unique list of elements and keep them sorted implicitly. It uses the self-balancing red-black tree, providing O(logN) complexity for insert, update or delete operation. It silently suppress  the attempt to add duplicate elements. It has numbers of handy method and property associated with it. Let me take simple examples and discuss each one of them.

First let me take a simple example to demonstrate how it internally maintain's the sorted order and removes the duplicate elements from the collection.

namespace SortedSetinCSharp4

{

    using System;

    using System.Collections.Generic;

    class Program

    {

        static void Main(string[] args)

        {

            SortedSet<int> sortedInt = new SortedSet<int>() { 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };

            foreach (var item in sortedInt)

            {

                Console.WriteLine(item);

            }

            Console.ReadKey();

        }

    }

}

Output: 1,2,3,4,5,7,8,9,10

As you can see from the output data is already sorted and duplicate element are removed.

SortedSet<T>.Reverse()

The Reverse method on SortedSet as the name suggest reverse the sort order.

namespace SortedSetinCSharp4

{

    using System;

    using System.Collections.Generic;

    class Program

    {

        static void Main(string[] args)

        {

            SortedSet<int> sortedInt = new SortedSet<int>() { 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };

            foreach (var item in sortedInt.Reverse())

            {

                Console.WriteLine(item);

            }

            Console.ReadKey();

        }

    }

}

Output: 10,9,8,7,5,4,3,2,1

SortedSet<T>.Remove()and SortedSet<T>.RemoveWhere

Remove method accepts an element of type T as its parameter and returns a bool value indicating the success or failure of the remove operation. While RemoveWhere is used to remove a group of elements from the collection satisfying the predicate defined. It return an integer value which is equal to the count of number of elements removed from the collection. I will demo the code for RemoveWhere. Remove you can try by yourself.

namespace SortedSetinCSharp4

{

    using System;

    using System.Collections.Generic;

    class Program

    {

        static void Main(string[] args)

        {

            SortedSet<int> sortedInt = new SortedSet<int>() { 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };

           int deleteCount = sortedInt.RemoveWhere(st => st % 2 == 0);

           Console.WriteLine(deleteCount);

            foreach (var item in sortedInt)

            {

                Console.WriteLine(item);

            }

            Console.ReadKey();

        }

    }

}

Output: 4

1,3,5,7,9

If you observe the sortedInt collection has five element that satisfy the predicate (st => st % 2 == 0), but the output shows count as 4 and not 5. This is best example to demonstrate that SortedSet<T>  implicitly removes the duplicate elements.

SortedSet<T>.Add(Titem) and SortedSet<T>Clear()

As the name suggest SortedSet<T>.Add(Titem) method is used to insert new element to the collection. It returns a bool value indicating if add operation was success or failure. If you try to add a duplicate value to the collection, it will not be added, this is clear from the false as return value while inserting duplicate value.

SortedSet<T>Clear() method is used to clear out all the elements of the collection.

SortedSet<T>.Maxand SortedSet<T>.Min

SortedSet<T>.Max and SortedSet<T>.Min are two important property. As the name suggest, they are used to get the maximum and minimum value of the collection.

namespace SortedSetinCSharp4

{

    using System;

    using System.Collections.Generic;

    class Program

    {

        static void Main(string[] args)

        {

            SortedSet<int> sortedInt = new SortedSet<int>() { 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };

            Console.WriteLine("{0}, {1}",sortedInt.Max, sortedInt.Min);

            Console.ReadKey();

        }

    }

}

Output: 10, 1

SortedSet<T>.GetViewBetween(TlowerValue, T upperValue)

This method returns a view of original collection, so any change that we make to this view will be reflected in the original collection            

namespace SortedSetinCSharp4

{

    using System;

    using System.Collections.Generic;

    class Program

    {

        static void Main(string[] args)

        {

            SortedSet<int> sortedInt = new SortedSet<int>() { 2, 5, 4, 9, 3, 2, 8, 10, 7, 1 };

            var result = sortedInt.GetViewBetween(3, 7);

            foreach (var item in result)

            {

                Console.WriteLine(item);

            }

            result.Add(6);

            foreach (var item in sortedInt)

            {

                Console.WriteLine(item);

            }

            Console.ReadKey();

        }

    }

}

Output: 3,4,5,7

Output: 1,2,3,4,5,6,7,8,9,10

 

As you can see from the above example, we added 6 to the result and the same is reflected in the original collection.

These are few of the main methods, properties associated with the SotedSet<T>.  Let me give a brief outline of other methods and you can explore them in detail.

Contains : Used to search for given element in the set

SortedSet<T>.CopyTo(T[] array, intindex, int count): Copy the specifiednumber of element from the original set to given array.

SortedSet<T>.Count: Count of element present in the collection

Happylearning J

Related Blogs:

Optional and Named Parameters in C# 4.0



Add Comment

Comment
Name
 (Required)
E-mail (Privacy assured)
 (Required)

Poll of the Day

  Who is best CEO so far?
 
 
 

Related Contents

Online Test Papers

  • ASP.NET
  • JavaScript
  • LINQ
  • SharePoint
  • Silverlight
  • SQL

Related Tutorials

  • HTML 5 Tutorial
  • SharePoint Best Practice
  • ASP.NET 4.0 Tutorials

Connect with Facebook

Top Blogs

  • SharePoint 2010 Model Dialog
  • Validation of viewstate MAC failed. If this application is hosted...
  • Silverlight: ListBox with CheckBox
  • Creating a Windows Service in C#
  • Using InstallUtil to install/uninstall service
  • How to call webservice using Ajax or JavaScript
  • LINQ to SQL Best Practice
  • The contract name could not be found in the list of contracts implemented by the service
  • Calling JavaScript function from C#
Blogs
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • HTML
  • JavaScript
  • LINQ
  • SQL Server
  • SharePoint
  • Silverlight
Tutorials
  • HTML5
  • ASP.NET 4.0
  • CSharp
  • SharePoint
  • SQL Server
Online Examinations
  • ASP.NET
  • LINQ
  • SQL Server
  • JavaScript
  • SharePoint
Online Utilities
  • Age Calculator
  • Happy Birthday Wisher
  • Independence Day Wisher
  • URL Encoder Decoder
  •  
Fun @ Work
  • Appraisal
  • Funny Images
  • Funny Puzzle
  • Miscellaneous
  • Motivating Stories
A2ZMenu © 2012 Home | Contact Us | Privacy Policy | Connect with Facebook | Follow us on Twitter