Wednesday, 15 February 2012

c# - Cannot implicitly convert type 'void' to 'System.Collections.Generic.Dictionary -



c# - Cannot implicitly convert type 'void' to 'System.Collections.Generic.Dictionary<string,bool> -

this code works fine

dictionary<string, bool> test = new dictionary<string, bool>(); test.add("test string", true);

the next code throws error : cannot implicitly convert type 'void' 'system.collections.generic.dictionary

dictionary<string, bool> test = new dictionary<string, bool>().add("test string", true);

why? difference?

the homecoming type of .add void

if chaining calls, lastly look becomes homecoming value of whole statement.

the homecoming value of new dictionary<k, v>() dictionary<k, v>, phone call .add on , .add returns nil (void)

you can utilize object initialiser syntax inline:

dictionary<string, bool> test = new dictionary<string, bool> { { "test string", true } };

edit: more info, lot of fluent syntax style frameworks homecoming object called method on allow chain:

e.g.

public class somefluentthing { public somefluentthing dosomething() { // stuff homecoming this; } public somefluentthing dosomethingelse() { // stuff homecoming this; } }

so can chain naturally:

somefluentthingvariable.dosomething().dosomethingelse();

c# generics dictionary

No comments:

Post a Comment