3

why in c# we need to initialize primitive type variable --

static void Main(string[] args)
{
   int a;
   Console.WriteLine(a);
}

throws compile time error ...

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263

4 Answers4

5

In order to prevent potential coding mistakes, C# will not allow you to use any local variable until the compiler can prove that it has been initialized.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Purely because it is a good practice. CLR initialize them to defaults anyway - for ValueTypes.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
0

its because of inference feature.... c # does type cast variable static unlike dynamic in python pearl so by checking type of values by which your variable is initialized type of variable is decided and verified. its done at compile time

0

There's another conceptual reason.

Everything in .NET is an object.

Variables are holders, which can hold references to objects or they can point to values.

Why C# should be able to let you output to Console (as in your example) a variable which holds nothing?

For me the question is, don't you find useful C# compiler preventing you from creating useless code? Any help is always useful in terms of avoiding human mistakes or wrong logic.

Maybe I should believe you say that because...

int a;

if(false) { a = 1; }

Console.WriteLine(a);

...won't compile too.

Well, why C# compiler should allow you to work with a reference to nothing? Any argument would be weak since what good program should write to console nothing? If your program doesn't need to print, just don't print.

For example, if you want to do it right, it should be:

int a;

if(false) { a = 1; } else { a = 0; }

Console.WriteLine(a);

"I want my program to show 1 if it's false, or 0 if it's true". But "show 1 if it's false, or show 'I don't know what'" is a weak argument.

It's like saying "do you want apples or emptiness?".

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 2
    An integer is a value type, not a reference type. It does have a default value, it's not pointing to anything. – Harry Feb 09 '11 at 14:00
  • But for .net, and not talking about low-level things, anything is an object. I tried to explain in a very abstract way. – Matías Fidemraizer Feb 09 '11 at 14:31
  • 1
    I see where you're going with this, but this is full of mis-statements. A variable is not a reference to an *object*. A variable is defined as a reference to a *storage location*. A storage location holds a *value*; a value may either be a value of a value type, or a reference to an instance of a reference type. Null is *not* an object, null is an *invalid reference*; it is a *value*. And finally, your logic is specious; why does the same logic not apply to fields, or to array elements, neither of which are required to be initialized explicitly? – Eric Lippert Feb 09 '11 at 15:21
  • Well you're absolutely right. I wanted to explain it in a "human language" so he can assume why C# works this way. – Matías Fidemraizer Feb 09 '11 at 15:26
  • @Eric I've changed "some" text so it reflects your comments. – Matías Fidemraizer Feb 09 '11 at 15:28