Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> C# >> C# SortedList Collection
C# SortedList Collection

Sample Program that uses SortedList [C#]
Views: 1054     Comments: 0
by Uttam Misra on 5/18/2012

This below example uses the parameterless constructor, the Add instance method, the ContainsKey and TryGetValue methods, the indexer, the enumerator, the IndexOfKey and IndexOfValue methods, and finally the Count property.

Sample Program that uses SortedList [C#]

 

using System;

using System.Collections.Generic;

 

class Program

{

    static void Main()

    {

            //
            // Created SortedList with keys and values.
            //

            SortedList<string, int> sorted = new SortedList<string, int>();
            sorted.Add("Game1", 1);
            sorted.Add("Game2", 2);
            sorted.Add("Game3", 3);
            sorted.Add("Game4", 4);
            sorted.Add("Game5", 5);

            //
            // Test SortedList with ContainsKey method.
            //

            bool contains1 = sorted.ContainsKey("Game1");
            Console.WriteLine("contains Game1 = " + contains1);

            //
            // Use TryGetValue method.
            //

            int value;
            if (sorted.TryGetValue("Game1", out value))
            {
                Console.WriteLine("Game1 key is = " + value);
            }

            //
            // Use item indexer.|
           //

            Console.WriteLine("Game2 key is = " + sorted["Game2"]);

            //
            // Loop over SortedList data.
            //

            foreach (var pair in sorted)
            {
                Console.WriteLine(pair);
            }

            //
            // Get index of key and then index of value.
            //

            int index1 = sorted.IndexOfKey("Game3");
            Console.WriteLine("index of Game3 (key) = " + index1);|
           int index2 = sorted.IndexOfValue(3);
            Console.WriteLine("index of 3 (value) = " + index2);

            //
            // Display Count property.
            //

            Console.WriteLine("count is = " + sorted.Count);

        }

}

Output

contains java = True
Game1 key is = 1
Game2 key is = 2
[Game1, 1]
[Game2, 2]
[Game3, 3]
[Game4, 4]
[Game5, 5]
index of Game3 (key) = 2
index of 3 (value) = 2
count is = 5

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Uttam MisraRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2663
  
Copyright © 2013MindStick. All Rights Reserved.