-1
if (i==101)
    {
        var lastLoopCheck;
    }

Visual Studio 2013 displays an error:- "Implicitly-typed local variables must be initialized"

Why? What's the reason for this?

My first language is JavaScript, in which uninitialized variables (both local and global) are allowed; take a look at the following (JavaScript) code:

var foo;
alert(typeof foo); //undefined

So why are uninitialized variables allowed in JavaScript and not in C#?

Ben
  • 51,770
  • 36
  • 127
  • 149
discussedtree
  • 233
  • 2
  • 4
  • 12
  • 1
    also see [Is there any way to create an implicitly typed global variable in C#?](http://stackoverflow.com/q/29130382/3899260) – discussedtree Mar 19 '15 at 05:35
  • 1
    Check out http://stackoverflow.com/questions/961581/whats-the-difference-between-dynamicc-4-and-var - you have some serious misunderstanding of `var` in C# (which is *very* different from `var` in JavaScript). – Alexei Levenkov Mar 19 '15 at 05:41
  • The thing variables in C# have that javascript doesn't is type. All variables need a type in a statically typed language, C# is one. – Jeff Mercado Mar 19 '15 at 05:43
  • Please read the edit. I would also like to bring your attention to the fact that I (the asker) have linked in the comments the question this is allegedly a duplicate of. Why would I post a duplicate question of a question I just posted 9 hours ago? It is beyond my understanding by what logic this has been marked as a duplicate of that. – discussedtree Mar 19 '15 at 05:50
  • 1
    Found much better duplicate for you - http://stackoverflow.com/questions/8457813/difference-between-the-implementation-of-var-in-javascript-and-c-sharp. I do believe that deep inside your real question is "why C# used `dynamic` instead of `var`", but "Difference between implementation of var in C#/JavaScript" should be good substitute. – Alexei Levenkov Mar 19 '15 at 06:04
  • 1
    Side note: the banner you were talking about was visible *only to you* to help you clarify your question (which you did, also blaming others for misunderstanding posts is totally optional on SO) – Alexei Levenkov Mar 19 '15 at 06:08
  • Now the banner says "This question already has an answer here:", where there are two links. This is only the duplicate of the first one, not the second one. – discussedtree Mar 19 '15 at 06:18
  • Yes - because when question is actually closed as duplicate all suggested duplicates are listed. – Alexei Levenkov Mar 19 '15 at 06:25
  • -1 for assuming language similarities based on keywords and (apparently) not reading any documentation prior to asking this question. Also, OP should make himself acquainted with [SO rules](http://stackoverflow.com/help). – LightBulb Mar 19 '15 at 06:57

1 Answers1

8

C# allows you to use var to specify that the type will be evaluated some other way than explicitly stating it. That's quite handy for code that used to look like this:

Type<string,int,int,int,int> xyzzy = new Type<string,int,int,int,int>();

Now you can instead do:

var xyzzy = new Type<string,int,int,int,int>();

However, the variable itself is still statically typed, so the type must be available at the point where the variable is created, so that the compiler can know what to do with it.

The clue lies in the error message you see:

Implicitly-typed local variables must be initialized

Note that it's implicitly-typed rather than untyped.

So, of these:

int x;       // explicit int.
var x = 7;   // implicit int because we're using int to set it.
var x;       // no idea what type this should be.

the first two are okay because the type information is available. The third is not okay because the information as to what type you want is not available.

Contrast the use of var with dynamic - the latter is more closely related to var in Javascript, it's dynamically typed(a) and figuring out what can be done to it is deferred until run-time.


(a) Technically, I think it's still considered a static type but the regular type checking is bypassed at compile time.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953