Don't use the object type with
dictionary and list. The point behin
generics is that you can work strongly
typed. If you want untyped
collections, use
System.Collections.Hashtable and
System.Collections.ArrayList. but I
would recommend using the generic
versions and using the correct types.
– Maximilian Mayerl
Hmm, tnx for the comment. I'm sure I will use same data types for keys and values (so, static) (e.g: Key: string only, value int only etc.) more than dynamic ones. Then, which is better for performance and coding?:
var a = new Dictionary<string, int> {{"one", 1}, {"two", 2}};
var b = new List<string> {"first", "second"};
var c = new Hashtable() {{"one", 1}, {"two", 2}};
var d = new ArrayList() {"first", "second"};
I have a thing that can be made with collections:
private static string City()
{
switch (region)
{
case "AL": return "Alabama";
case "AK": return "Alaska";
case "AZ": return "Arizona";
case "AR": return "Arkansas";
case "CA": return "California";
case "CO": return "Colorado";
case "CT": return "Connecticut";
case "DE": return "Delaware";
case "FL": return "Florida";
case "GA": return "Georgia";
case "HI": return "Hawaii";
case "ID": return "Idaho";
case "IL": return "Illinois";
case "IN": return "Indiana";
case "IA": return "Iowa";
case "KS": return "Kansas";
case "KY": return "Kentucky";
case "LA": return "Louisiana";
case "ME": return "Maine";
case "MD": return "Maryland";
case "MA": return "Massachusetts";
case "MI": return "Michigan";
case "MN": return "Minnesota";
case "MS": return "Mississippi";
case "MO": return "Missouri";
case "MT": return "Montana";
case "NE": return "Nebraska";
case "NV": return "Nevada";
case "NH": return "New Hampshire";
case "NJ": return "New Jersey";
case "NM": return "New Mexico";
case "NY": return "New York";
case "NC": return "North Carolina";
case "ND": return "North Dakota";
case "OH": return "Ohio";
case "OK": return "Oklahoma";
case "OR": return "Oregon";
case "PA": return "Pennsylvania";
case "RI": return "Rhode Island";
case "SC": return "South Carolina";
case "SD": return "South Dakota";
case "TN": return "Tennessee";
case "TX": return "Texas";
case "UT": return "Utah";
case "VT": return "Vermont";
case "VA": return "Virginia";
case "WA": return "Washington";
case "WV": return "West Virginia";
case "WI": return "Wisconsin";
case "WY": return "Wyoming";
}
return City();
}
EDIT:
var ren = new System.Diagnostics.Stopwatch();
ren.Start();
var a = new Dictionary<string, int> {{"asd", 1}, {"f", 1}};
var b = new List<string> {"asd", "asdf"};
ren.Stop();
var rn1 = ren.Elapsed.TotalMilliseconds;
ren.Reset();
ren.Start();
var c = new Hashtable() {{"asd", 1}, {"f", 2}};
var d = new ArrayList() {"asd", "asdf"};
ren.Stop();
var rn2 = ren.Elapsed.TotalMilliseconds;
ren.Reset();
ren.Start();
var e = new Dictionary<object, object> { { "asd", 1 }, { "f", 1 } };
var f = new List<object> { "asd", "asdf" };
ren.Stop();
Console.WriteLine("Dictionary<string, int> + List<string> = {0} Miliseconds"
+ "\nHashtable() + ArrayList() = {1} Miliseconds\nDictionary<object,"
+" object> + List<object> = {2} Miliseconds", rn1, rn2,
ren.Elapsed.TotalMilliseconds);
/*
Returned (Generally the results will be similar):
Dictionary<string, int> + List<string> = 0,1331 Miliseconds
Hashtable() + ArrayList() = 0,0145 Miliseconds
Dictionary<object, object> + List<object> = 0,0567 Miliseconds
*/
Looks Hashtable and Arraylist is the winner :D (Lol). And the "object" type was better than string and integer. They are almost the same (in coding), so, I will use HashTable and ArrayList...