c# - Why double declaration is needed while defining variable Types? -
i newbie c#, , 1 thing cant understand @ moment, why types needs written twice, i.e.
dictionary<string, string> lines = new dictionary<string, string>();
why couldnt simpler, like:
lines= new dictionary<string, string>(); // or dictionary<string, string> lines = ();
doesnt "lines" variable? have been easier.
if code inside method, why the var
keyword has existed since c# 3.0:
var lines = new dictionary<string, string>();
if in class declaration, can't use var
keyword. admit not knowing specifics of why implicit typing isn't permitted @ class level, suffice isn't. explicit typing required:
private dictionary<string, string> lines;
completely separate declaration initialization of variable. can done on same line, or in constructor, or in method, etc. new
keyword going need explicitly told type:
new dictionary<string, string>()
these 2 different things both require type specified own discrete , reasons.
ultimately, time ask "why doesn't language this?" should really asking "should language this? necessary?"
as eric lippert once said me in previous question: "by eliminating [that unnecessary feature], none of rules [that unnecessary feature] needed though of, argued about, designed, specified, implemented, tested, documented, shipped customers, or made compatible every future feature of c#."
it's easy shout bleachers. it's less easy design , support major programming language. maybe 1 day feature exist, , you're welcome propose team. "why doesn't exist", team never made happen.
Comments
Post a Comment