using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary dictionary = new Dictionary();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
}
}
using System.Collections.Generic;
class Program
{
static void Main()
{
var values = new Dictionary();
values.Add("A", "uppercase letter A");
values.Add("c", "lowercase letter C");
// Use inline "out string" with TryGetValue.
if (values.TryGetValue("c", out string description))
{
System.Console.WriteLine(description);
}
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Example Dictionary again.
Dictionary d = new Dictionary()
{
{"cat", 2},
{"dog", 1},
{"llama", 0},
{"iguana", -1}
};
// Loop over pairs with foreach.
foreach (KeyValuePair pair in d)
{
Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}
// Use var keyword to enumerate dictionary.
foreach (var pair in d)
{
Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary d = new Dictionary()
{
{"cat", 2},
{"dog", 1},
{"llama", 0},
{"iguana", -1}
};
// Store keys in a List.
List list = new List(d.Keys);
// Loop through list.
foreach (string k in list)
{
Console.WriteLine("{0}, {1}", k, d[k]);
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
static void Main()
{
var test = new Dictionary();
test["bird"] = 10;
test["frog"] = 20;
test["cat"] = 60;
int sum = 0;
const int _max = 1000000;
// Version 1: use foreach loop directly on Dictionary.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
foreach (var pair in test)
{
sum += pair.Value;
}
}
s1.Stop();
// Version 2: use foreach loop on Keys, then access values.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
foreach (var key in test.Keys)
{
sum += test[key];
}
}
s2.Stop();
Console.WriteLine(s1.Elapsed.TotalMilliseconds);
Console.WriteLine(s2.Elapsed.TotalMilliseconds);
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Use a Dictionary with an int key.
Dictionary dict = new Dictionary();
dict.Add(100, "Bill");
dict.Add(200, "Steve");
// We can look up the int in the Dictionary.
if (dict.ContainsKey(200))
{
Console.WriteLine(true);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] arr = new string[]
{
"One",
"Two"
};
var dict = arr.ToDictionary(item => item, item => true);
foreach (var pair in dict)
{
Console.WriteLine("{0}, {1}",
pair.Key,
pair.Value);
}
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary d = new Dictionary();
d.Add("cat", 1);
d.Add("dog", 2);
if (d.ContainsValue(1))
{
Console.WriteLine(true); // True.
}
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary dictionary = new Dictionary();
// We can assign with the indexer.
dictionary[1] = 2;
dictionary[2] = 1;
dictionary[1] = 3; // Reassign.
// Read with the indexer.
// ... An exception occurs if no element exists.
Console.WriteLine(dictionary[1]);
Console.WriteLine(dictionary[2]);
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary d = new Dictionary();
d.Add("cat", 1);
d.Add("dog", 2);
d.Remove("cat"); // Removes cat.
d.Remove("nothing"); // Doesn't remove anything.
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Example e = new Example();
Console.WriteLine(e.GetValue());
}
}
class Example
{
Dictionary _d = new Dictionary()
{
{1, 1},
{2, 3},
{3, 5},
{6, 10}
};
public int GetValue()
{
return _d[2]; // Example only.
}
}