Do I have to “double declare” every new instance in c#?
Obj sb = new Obj();
VB is cheaper
Dim sb as new Obj()
and Python cheapest
sb=Obj()
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, as of C# 3 you can use
varfor local variables:Note that this is very different from the Python declaration: the variable
xis still of typeDictionary<string, string>; it’s just that the compiler has inferred the type from the right-hand side of the assignment. So you’ll still get IntelliSense support and all the other benefits of static typing. (If you want dynamic typing, you can usedynamicas of C# 4, but that’s a very different feature.)This feature was partly added to support anonymous types, although it’s very useful in other cases too; most notably when you are calling a constructor.
A few things to bear in mind:
The compiler has to be able to infer a concrete type from the assignment; you can’t write
for example.