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#

New to C#, but I reckon in JavaScript the var-keyword is outdated. Is this the same case for C#?

Hi there, as I am watching this lesson, I am wondering if the variable keyword var is the preferred keyword over let. I know that in JavaScript, developers prefer to use let instead of var, as this is new industry standards. Is it just because the video was made some time ago, or is it because C#-developers actually use var instead of let?

2 Answers

Steven Parker
Steven Parker
231,269 Points

When learning a second (or subsequent) language, it's important to remember that the same keyword might be used for very different things. The "var" keyword is a good example between JavaScript and C#, though in both languages it should be used where appropriate for the context. But there have been no new developments in C# that would affect where "var" might be used.

And "var" isn't obsolete in JavaScript, though in most cases the newer "const" or "let" might be a better choice. But if automatic initialization, function scoping or hoisting are needed, the newer terms won't do.

Tomas Schaffer
Tomas Schaffer
11,606 Points

If you defining variable in JavaScript var/const/let difference is in the scope and posibility to change the value of variable as far i know. In C# you can define as below:

var example = 1;
var example2 = "Monday" OR

int example = 1; string example2 = "Monday"

both is the same and correct but the second is better practice to use. In first case C# define automaticly the Type of variable in second you define which type it should be.