Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Var?

Why would you use var instead of double for example. Doesn't var make the code more diffcult to read?

1 Answer

Steven Parker
Steven Parker
229,732 Points

Using "var" just lets the compiler determine the type for you. In many cases, a variable is being declared and initialized at the same time, so the type is obvious to both the compiler and the reader from what is being assigned to it. I personally think the code can be easier to read at those times.

But if the type isn't clear at first glance, even if the compiler can determine it, I think you have a strong case for using a explicit type declaration for readability.

Happy coding!