views:

108

answers:

1

I have the following code that splits a string on newlines and converts it to a Dictionary for further processing:

        string[] splitProgram = program.Split(Environment.NewLine.ToCharArray());
        short i = 0;
        Dictionary<short, string> programDictionary = splitProgram.ToDictionary<short, string>((value) => i++);

What's odd is i get the following errors on the third line:

Error 1 Instance argument: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable<short>'
Error 2 'string[]' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' has some invalid arguments
Error 3 Argument '2': cannot convert from 'lambda expression' to 'System.Func<short,string>'

I'm absolutely stumped on this and can't figure it out. Can someone help?

+3  A: 

Did you notice the order of Source and Key in ToDictionary<TSource,TKey> ?

You could try :

//untested
... = splitProgram.ToDictionary<string, short>((value) => i++);
Henk Holterman
Didn't notice that at all, thanks a bunch! i am a bit tired at the moment though so...
RCIX
That's why it's better to leave it to the compiler: splitProgram.ToDictionary(value => i++);
Darin Dimitrov