C# How does Dictionary work when we initialize a class within as a value? -


assuming have following code:

public static dictionary<string, viewmodelbase> loaddictionary() {     dictionary<string, viewmodelbase> tempdictionary = new dictionary<string, viewmodelbase>();     tempdictionary.add("loginview", new loginviewmodel());     tempdictionary.add("signupview", new signupviewmodel());     tempdictionary.add("contactlistview", new contactlistviewmodel());     return tempdictionary; } 

i refer line:

dictionary<string, viewmodelbase> tempdictionary = new dictionary<string, viewmodelbase>(); 

does compiler first create constructor (parameterless ctor) , add keyvaluepairs?

if so, how parameter ctor (of loaddictionary)?

and important question relevant post is:

when add keyvaluepairs, values instantiated or wait called , instantiated?

i mean to:

new loginviewmodel() new signupviewmodel() new contactlistviewmodel() 


edit:

i want know if

tempdictionary.add("loginview", new loginviewmodel()); 

would executed , call loginviewmodel constructor if did not call "loginview" key in other part of program.

does compiler first creates constructor (parameterless ctor) , add keyvaluepairs?

the compiler doesn't have create anything. dictionary<> has parameterless constructor, you're calling here:

new dictionary<string, viewmodelbase>(); 

you're invoking constructor, passing no parameters, , getting instance of dictionary<string, viewmodelbase>.

when add keyvaluepairs, values instantiated?

they're instantiated, because you're instantiating them:

new loginviewmodel() 

like other constructor, creates new instance of class.


note none of code you're showing has class initializers. initializer collection type (like dictionary<>) might this:

dictionary<string, viewmodelbase> tempdictionary = new dictionary<string, viewmodelbase>() {       {"loginview", new loginviewmodel()},      {"signupview", new signupviewmodel()},       {"contactlistview", new contactlistviewmodel()}   } 

which compiled equivalent of have:

dictionary<string, viewmodelbase> tempdictionary = new dictionary<string, viewmodelbase>(); tempdictionary.add("loginview", new loginviewmodel()); tempdictionary.add("signupview", new signupviewmodel()); tempdictionary.add("contactlistview", new contactlistviewmodel()); 

it's not entirely clear confusion on subject, of code create instances of objects.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -