Using type inference we can write:
var myList = new List<int>();
but it’s not really helping is it? Because the still requires us to only add ints, so wouldnt it have been easier to just declare the variable of ‘int’ instead of ‘var’ ?
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.
The
vardoesn’t change the output of the compiler – it only makes the code shorter.The only time that
varoffers anything else is in regards to anonymous types:It is important to understand that
vardoesn’t mean dynamic or variable (in the sense that it allows the type of the object to vary) – it just allows you to declare a variable and have the compiler infer the type as if you had declared it.