0

if variable is not assigned, then it takes the default value at run time. for example

int A1;

if i will check the value of A1 at runtime it will be 0; then why at compile time it throws a error of unassigned value; why CLR don't use to a lot the default value at runtime;

int A1;
int B1 = A1+10;

it should be 11 as the default value of A1 is 0;

there project property where i can check for "assign default values for unassigned variable";

Can anybody tell me where i can find it?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
slash shogdhe
  • 3,943
  • 6
  • 27
  • 46

6 Answers6

3

Your statement

if variable is not assigned,then it takes the default value at run time

is only true for member variables in a class.

For local variables inside a function, this is wrong. Local variables inside a function always require initialization.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • thats what i am asking,why it is like so – slash shogdhe Dec 23 '10 at 11:54
  • Probably you have to read the language specification of C# for the reason behind this. You'll find it at http://www.ecma-international.org/publications/standards/Ecma-334.htm Since you will not be able to change this, I just would remember the rule and make my code comply to it. – Uwe Keim Dec 23 '10 at 12:01
  • thankx alot for link,even i am going through the http://msdn.microsoft.com/en-us/library/aa691172(VS.71).aspx but this is for .net 2003 and i am using 2010.if u find the latest thing plz forword me – slash shogdhe Dec 23 '10 at 12:15
1

it should be 11 as the default value of A1 is 0;

This is exactly the reason that the C# compiler won't let you get away with using uninitialized variables. The result would be 10, not 11. After a good 30 years of experience with C and C++, languages that allow you to use uninitialized variables, the C# team decided that this was a major source of bugs and to not allow this in a C# program.

There are lots of little tweaks like this. Another great example is not allowing fall through to another case in a switch statement. Forgetting to write break is such a classic bug. Outlawing these C-isms is rather an excellent idea and a big part of why C# is such a great language. Unless you dislike the idea of a compiler as a police officer.

Fwiw: using an uninitialized variable is permitted in VB.NET.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Most of the time this happens is because the variable is an Object and before you can use it you need to Instantiate it.

When you assign a String = "" this is instantiated for you

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
0

You are talking about local variables or class level variables ? The rules are different for both. Check our Jon Skeet's reply at this:

Initialization of instance fields vs. local variables

Community
  • 1
  • 1
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
0

Default value is true for class members, but not for function locals. Whatever code you put directly into an as[pc]x file, the code generator will put it into a function.

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • yeah i know its valid for call members but y not not local variable – slash shogdhe Dec 23 '10 at 11:56
  • thats what i am saking y it is like so. – slash shogdhe Dec 23 '10 at 12:08
  • It would be too difficult to check whether class members are initialized or not. They could be set from functions, properties, constructors, you never know. Function locals, however, can only be initialized inside the function, and this can be easily verified. It wouldn't be too hard for Microsoft to automatically initialize them to defaults, too, instead it's their way of forcing you to initialize them so that your code looks clean. – fejesjoco Dec 23 '10 at 12:30
  • so we can think that this is the limitatin – slash shogdhe Dec 23 '10 at 12:41
  • Absolutely. Sometimes it doesn't even work perfectly, for example when I define a local, then I run a loop, at some point I assign a value to the variable, and exit the loop, but the compiler cannot determine for sure that the variable will ever be initialized and I'm forced to assign a default value. In any case, if you assign defaults, the code is easier to read and verify. So it's a good practice actually. – fejesjoco Dec 23 '10 at 12:46
  • thankx alot,its really nice what u have told about looping,just i have tested it, and now i am sure its a .net limitation – slash shogdhe Dec 23 '10 at 13:00
  • well this is becoz its always not necessory that the statement under loop will execute.....just like if without else .......... for (int j = 0; j < 0; j++) statement under this loop will never execute – slash shogdhe Dec 23 '10 at 13:08
0

The heap (reference classes) and the constructor for structs zero's the data.

Simple value-types like int, but also references (=pointers) to objects, do not get a default value on the stack. You should always set it. If this was not mandatory, specially with object-pointers, this could be a major security breach, because you are pointing to unknown locations.

Any default value (like 0) would probably be wrong 50% of the time.

GvS
  • 52,015
  • 16
  • 101
  • 139
  • Sorry but this is not the answer of my question,and what ever u are tring to explain is also wrong.well its not the matter of reference type and value type.as both have there own default value. – slash shogdhe Dec 23 '10 at 12:12
  • If you examine your app with a debugger, you will see that stack-based variables do not have a default value. – GvS Dec 23 '10 at 12:17
  • it will have have a default value. – slash shogdhe Dec 23 '10 at 12:39
  • Ok, the defaultvalue (of stack based variables = local variables) is what is leftover from the previous (stack) operation. Not really the value I would accept as a default value. For structs this is different, because they should be initialized by the constructor. If you do not initialize them, their value is also unpredictable. – GvS Dec 23 '10 at 14:38