ordereddictionary ordered = new ordereddictionary(); ordered.add('a', "something"); ordered.add('b', "someone"); ordered.add('c', "somewhere"); foreach (char character in ordered.keys) { messagebox.show(ordered[character].tostring()); }
i cannot items key. code throws
index out of range. must non-negative , less size of collection.
it takes char 'a' 97 integer value , tries value index reason this?
if use object
in loop ınstead works. why?
foreach (object character in ordered.keys) { messagebox.show(ordered[character].tostring()); }
ordereddictionary has 2 overloads indexer: 1 take int
index , other takes object
.
c# trying convert char
type fit 1 of overloads , 1 choosing convert char
int
(because compiler sees "easier" do) , makes assumption accessing index. solution explicit:
ordered[(object)character].tostring());
Comments
Post a Comment