C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: This code only needs to be written once if you have it use the IDictionary type.
Next: The WriteKeyA method works equally well on Dictionary and SortedDictionary instances.
C# program that uses IDictionary type
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Dictionary implements IDictionary.
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict["A"] = "B";
WriteKeyA(dict);
// SortedDictionary implements IDictionary.
SortedDictionary<string, string> sort =
new SortedDictionary<string, string>();
sort["A"] = "C";
WriteKeyA(sort);
}
static void WriteKeyA(IDictionary<string, string> i)
{
// Use instance through IDictionary interface.
Console.WriteLine(i["A"]);
}
}
Output
B
C
Tip: You could even later implement a custom Dictionary type and never need to change this class. Variables can use type IDictionary.
Further: Even the alternatives in the .NET Framework, such as SortedDictionary, are typically not useful.
SortedDictionary