當前位置: 首頁>>代碼示例>>C#>>正文


C# SortedDictionary<TKey,TValue>構造函數代碼示例

本文整理匯總了C#中System.Collections.Generic.SortedDictionary<TKey,TValue>.SortedDictionary構造函數的典型用法代碼示例。如果您正苦於以下問題:C# SortedDictionary<TKey,TValue>構造函數的具體用法?C# SortedDictionary<TKey,TValue>怎麽用?C# SortedDictionary<TKey,TValue>使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。您也可以進一步了解該構造函數所在System.Collections.Generic.SortedDictionary<TKey,TValue>的用法示例。


在下文中一共展示了SortedDictionary<TKey,TValue>構造函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1:

// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith = 
    new SortedDictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is 
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:22,代碼來源:SortedDictionary

示例2: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new SortedDictionary of strings, with string keys 
        // and a case-insensitive comparer for the current culture.
        SortedDictionary<string, string> openWith = 
                      new SortedDictionary<string, string>( 
                          StringComparer.CurrentCultureIgnoreCase);
        
        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Try to add a fifth element with a key that is the same 
        // except for case; this would be allowed with the default
        // comparer.
        try
        {
            openWith.Add("BMP", "paint.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("\nBMP is already in the dictionary.");
        }
        
        // List the contents of the sorted dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:41,代碼來源:SortedDictionary

輸出:

BMP is already in the dictionary.

Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe

示例3: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys.
        //
        Dictionary<string, string> openWith = 
                                  new Dictionary<string, string>();
        
        // Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
        
        // Create a SortedDictionary of strings with string keys, 
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy = 
                  new SortedDictionary<string, string>(openWith);

        // List the contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
               kvp.Key, kvp.Value);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:33,代碼來源:SortedDictionary

輸出:

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe

示例4: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys and
        // a case-insensitive equality comparer for the current 
        // culture.
        Dictionary<string, string> openWith = 
            new Dictionary<string, string>
                (StringComparer.CurrentCultureIgnoreCase);
        
        // Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe");
        openWith.Add("Bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
        
        // List the contents of the Dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }

        // Create a SortedDictionary of strings with string keys and a 
        // case-insensitive equality comparer for the current culture,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string> copy = 
                    new SortedDictionary<string, string>(openWith, 
                        StringComparer.CurrentCultureIgnoreCase);

        // List the sorted contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:45,代碼來源:SortedDictionary

輸出:

Key = txt, Value = notepad.exe
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe

Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe


注:本文中的System.Collections.Generic.SortedDictionary<TKey,TValue>.SortedDictionary構造函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。