sorted list definition with example in C#.Net

This SortedList class comes under collections. Basically this list will store all elements in the order way. Using binary search, elements will search. Using this our development effort will reduce.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace sortedlist
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creates and initializes a new SortedList.
            SortedList objSortedList = new SortedList();
            objSortedList.Add("3 - third line", "Kumar");
            objSortedList.Add("2 -Second line", "Rajesh");
            objSortedList.Add("1 -First line", "Ranga");

            SortedListKeysAndValues(objSortedList);
            Console.ReadLine();
        }

        public static void SortedListKeysAndValues(SortedList sortedList)
        {
            Console.WriteLine("\t-KEY-\t\t-VALUE-");
            for (int i = 0; i < sortedList.Count; i++)
            {
                Console.WriteLine("\t{0}:\t{1}", sortedList.GetKey(i), sortedList.GetByIndex(i));
            }
            Console.WriteLine();
        }
    }
}


Output:

        -KEY-           -VALUE-
        1 -First line:  Ranga
        2 -Second line: Rajesh
        3 - third line: Kumar



Please give your valuable articals on this topic. We will post by the name of you, if you like. Please submit your articles in the comments.

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar