Make your method generic as well and then you'll be able to do what you're doing. You won't have to change your usage pattern since the compiler will be able to infer generic types from input types public IDictionary Copy(IDictionary source) { IDictionary targetDictionary = new Dictionary(); foreach (KeyValuePair sourcePair in sourceDictionary) { targetDictionary. Insert(sourcePair.
Key, sourcePair. Value); } return targetDictionary; } If you don't really need to convert it from IDictionary.
Make your method generic as well and then you'll be able to do what you're doing. You won't have to change your usage pattern since the compiler will be able to infer generic types from input types. Public IDictionary Copy(IDictionary source) { IDictionary targetDictionary = new Dictionary(); foreach (KeyValuePair sourcePair in sourceDictionary) { targetDictionary.
Insert(sourcePair. Key, sourcePair. Value); } return targetDictionary; } If you don't really need to convert it from IDictionary to IDictionary then you can use the copy constuctor of Dictionary which accepts another dictionary as input and copies all values--just like you're doing now.
I like this answer. It is concise. – karbon Feb 24 '10 at 6:15 I took out the "this doesn't compile" line because your version does compile.
Hope you don't mind! – Aaronaught Feb 27 '10 at 2:38 @Aaronaught, thanks – Samuel Neff Feb 27 '10 at 3:31.
This may be a fix for you but you'll need . Net 3.5 or greater to use the var keyword. // this should compile foreach (var sourcePair in sourceDictionary) { targetDictionary.
Insert(sourcePair. Key, sourcePair. Value); }.
1 var is a convenience method that lets the compiler infer the types from usage. This isn't possible in many of the paces where it's used above, like in the typeof() check or in the cast. – Samuel Neff Feb 24 '10 at 5:19 Agreed.
Var is using the same as using object at runtime. – karbon Feb 24 '10 at 6:19 1 @karbon, no,var is not like using object. Var is just like specifying the full type explicitly, just you don't do it, the compiler figures it out from code at compile time.
Var has zero effect on run-time code. – Samuel Neff Feb 27 '10 at 3:32.
Here is a method (don't leave it as static, unless you need it to be, I wrote it in a quick console app) that basically converts a Dictionary of any type to an object/object dictionary. Private static Dictionary DeTypeDictionary(Dictionary inputDictionary) { Dictionary returnDictionary = new Dictionary(); foreach(T key in inputDictionary. Keys) { if( (key is object) && (inputDictionarykey is object)) { returnDictionary.
Add(key, inputDictionarykey); } else { //sorry these aren't objects. They may be dynamics. Continue; } } return returnDictionary; } ...and here is how you use it... Dictionary d = new Dictionary(); d.
Add("rsgfdg", DateTime. Now); d. Add("gfdsgd", DateTime.
Now); Dictionary newDictionary = DeTypeDictionary(d).
Even with dynamics in . NET 4 it provides dynamic variables and arguments, but, afaik, not dynamic types for generic declarations. – Samuel Neff Feb 24 '10 at 5:46 You're right.
At compile time, dynamics are dynamics, but, like var, they become objects @ runtime, so at run time, they are always objects. – karbon Feb 24 '10 at 6:14.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.