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# C# Basics Numbers Math Operations

use of var in C#

A bit confused here, so i did some research on the matter. He uses var to declare a variable but, in the video without really explaining the implications for when to use and not to use. what i found (some of which)seems a bit more advanced for C# basics, so why lead us down that path and use it in the video at all without explaining?

Use of “var” is not recommended everywhere. The var was created to handle declarations when the type is not known, such as generic types, lambdas, and query expressions. If you already know the type of a variable, you must declare that explicitly. Remember, if you don’t declare a variable explicitly, the compiler must do extra work to determine the type. While the cost of this operation may not be significant, it’s just unnecessary burden on the compiler.
Don’t use var for simple local variable types that are known to you.
Here are some of the common uses of the var keyword.
Use of var when you’re not sure what type of data will be stored in a variable.
Use in anonymous types and anonymous collections.
Use of var improves code readability. Use when class names are extremely long.
Imported unmanaged code types that doesn’t follow naming conventions.

2 Answers

Steven Parker
Steven Parker
231,198 Points

While it doesn't agree with the recommendations you quoted, "var" is often used simply as a convenience when the type is obvious as is generally true in initializations.

The concept of "unnecessary burden on the compiler" is probably not a major concern of many developers and might even seem humorous to some.

just went back and reviewed "types" from the very beginning lessons he does cover this. I guess i missed it. Thank you for not rubbing that in my face! I will eat my humble pie!

I see that point of being humorous but, if you know the type you will be storing, why not just declare it from the start? Since it's a strongly typed language and there are few instances where you don't need to declare the data type you will be storing.

I think where the confusion is coming in is, in every instance up to this point when he declared a variable, he declared the type and went from there. In this video, he just throws in a var like it's something he's been doing in every video up to this point, without so much as an explanation. Now i can't focus.;) As I'm having to research more on this. He threw a wrench in my ebb and flow. my source was/is https://www.c-sharpcorner.com/article/what-does-var-mean-in-c-sharp/