2

Possible Duplicate:
Where and why use int a=new int?

What's the difference between these two below?

int i =0; 
int i = new int();

Is there any difference in case of memory allocation ? Is there any other difference?

Community
  • 1
  • 1
Joby Kurian
  • 3,699
  • 4
  • 30
  • 38
  • implicit and explicit declarations of variable "i". – Mullaly Nov 01 '12 at 05:38
  • @Mullaly -- how so? (FWIW: `0 == new int() == default(int)`) –  Nov 01 '12 at 05:38
  • 1
    The first one is obviously unambiguous, even to the casual reader. – tvanfosson Nov 01 '12 at 05:42
  • new int().. goes through the construcuor of int32 class. – Mullaly Nov 01 '12 at 05:42
  • 2
    @Mullaly No, it doesn't. System.Int32 is a "special" *structure* type, along with the other primitives. Just because it doesn't have the same awkward separation as Java doesn't mean the bytecode doesn't treat it special/efficiently! (Compare with `1+2` or other such operations.) –  Nov 01 '12 at 05:43
  • 1
    @pst..Thanks for enlightment.! – Mullaly Nov 01 '12 at 05:44

2 Answers2

5

Both of them compiles to same thing.

Suppose you have:

static void Main(string[] args)
{
    int i = 0;
    int j = new int();

    Console.Write("{0}{1}", i, j);
}

If you build in Release mode and see the executable in ILSpy, it compiles to:

private static void Main(string[] args)
{
      int i = 0;
      int j = 0;
      Console.Write("{0}{1}", i, j);
}

new int() is same as default(int)

Here is the IL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       27 (0x1b)
  .maxstack  3
  .locals init ([0] int32 i,
           [1] int32 j)
  IL_0000:  ldc.i4.0
  IL_0001:  stloc.0
  IL_0002:  ldc.i4.0
  IL_0003:  stloc.1
  IL_0004:  ldstr      "{0}{1}"
  IL_0009:  ldloc.0
  IL_000a:  box        [mscorlib]System.Int32
  IL_000f:  ldloc.1
  IL_0010:  box        [mscorlib]System.Int32
  IL_0015:  call       void [mscorlib]System.Console::Write(string,
                                                            object,
                                                            object)
  IL_001a:  ret
} // end of method Program::Main
Habib
  • 219,104
  • 29
  • 407
  • 436
0

The first one

int i = 0;

Initialize a new integer of name i. Then, sets its value to 0

While the second one

int i = new int();

Initializes a new integer of name i to the default value (which is 0). This is also similar to

int i = default(int);

Thanks,
I hope you find this helpful :)

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • 2
    In all cases the value is set to 0. It does more than just allocate space for the variable and create the symbol table entry. – tvanfosson Nov 01 '12 at 05:49
  • @tvanfosson I agree with you. There's no difference but I just wanted to illustrate what each does. It won't be the same in case the value changes :) – Picrofo Software Nov 01 '12 at 05:50
  • int i = new int(); this also sets as 0. – Joby Kurian Nov 01 '12 at 05:50
  • 1
    The phrase "without doing anything else" is misleading then. You seem to be saying that in the second case it doesn't set the value of the memory location, but C# will initialize it to the default value, 0. – tvanfosson Nov 01 '12 at 05:52
  • @JobyKurian Yes. Since you did not set a particular value, the value will be set automatically to zero. So that you'll have a defined variable. The same also happens with other structs such as `float`/`double`/etc... Have a great day :) – Picrofo Software Nov 01 '12 at 05:52
  • @tvanfosson By "without doing anything else" I meant that the value will be *default-ly* initialized and by initialized I mean that the value will be set to `0` if the programmer did not specify a specific value for the `int`. I did not mean that the value will not be set to `0` in case there's no specific input. Have a great day :) – Picrofo Software Nov 01 '12 at 05:56
  • I've changed the phrase to more accurately reflect what you've stated in your comments. – tvanfosson Nov 01 '12 at 05:58