Bastien Vandamme
Bastien Vandamme

Reputation: 18485

What is the difference between "int myInt;" and "int myInt = 0;"

Which C# statement is equivalent to this:

int myInt = new int();

I found this question on http://smarterer.com. The smarterer.com website suggest 3 answers:

I know the first proposition is incorrect. But what about the two other. My question now is what is the difference between int myInt; and int myInt = 0;? For me it is the same.

Upvotes: 0

Views: 1346

Answers (3)

tophallen
tophallen

Reputation: 1063

int is a struct, so when you define an int in C# it is technically initialized to it's default value (zero). However it is best practice to initialize a value on a field so the most equivalent of the three options you listed is the third one: int myInt = 0

You cannot assign a struct (value type) to null as it is a non-nullable type.

there are ways to have nullable structs(Nullable<T>) but that's not what you are asking about.

MSDN on default values

UPDATE:

As my post has some commenters that are sharing mixed results, I figured I would elaborate the percieved performance hit when you initialize valuetypes on delcaration instead of when you need them.

when you optimize your build (no nop commands in the IL code) you won't see your initialization if you are setting it to the default value, this is the test code:

class initfield
{
    public int mynum = 0;
}

class noinitfield
{
    public int mynum;
}

and this is the IL for the classes and invoking them (sorry if it is beyond the question scope):

with initialization:

.class private auto ansi beforefieldinit test.initfield
extends [mscorlib]System.Object
{
// Fields
.field public int32 mynum

// Methods
.method public hidebysig specialname rtspecialname 
    instance void .ctor () cil managed 
{
    // Method begins at RVA 0x2087
    // Code size 7 (0x7)
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: call instance void [mscorlib]System.Object::.ctor()
    IL_0006: ret
} // end of method initfield::.ctor

} // end of class test.initfield

without initialization:

.class private auto ansi beforefieldinit test.noinitfield
extends [mscorlib]System.Object
{
// Fields
.field public int32 mynum

// Methods
.method public hidebysig specialname rtspecialname 
    instance void .ctor () cil managed 
{
    // Method begins at RVA 0x208f
    // Code size 7 (0x7)
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: call instance void [mscorlib]System.Object::.ctor()
    IL_0006: ret
} // end of method noinitfield::.ctor

} // end of class test.noinitfield

now if you recompile that IL code back into C# for the class that was initializing the field:

internal class initfield
{
    public int mynum;
}

as you can see the compiler optimizes the redundant initialization to the default value. Note that this only applies to primitive types, so datetime does not get optimized away. - So in the case of the question asked, int myInt; and int myInt = 0; are exactly the same.

Upvotes: 1

Hasanka Rathnayake
Hasanka Rathnayake

Reputation: 137

In C# the default value of data type is using by new operator. int myint=new int(); statement is having the same effect as int myInt = 0;

See http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Upvotes: 0

Peter Duniho
Peter Duniho

Reputation: 70691

The second statement depends on context. If it's a field declaration, then it's no different from assigning 0, because type members are always initialized. If it's a local variable, then the second statement doesn't initialize the variable; it would be required for it to be initialized later before you use it.

You can't assign NULL to something of type int, so obviously the first answer isn't the right one. :)

I would choose the third answer, because it is the most reliably like using new int().

Upvotes: 9

Related Questions