35

I instantiate an array like this:

int array[] = new int[4];

What are the default values for those four members? Is it null, 0 or not exists?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
dylanmensaert
  • 1,591
  • 4
  • 23
  • 38

4 Answers4

57

It's 0. It can't be null, as null isn't a valid int value.

From section 7.6.10.4 of the C# 5 specification:

All elements of the new array instance are initialized to their default values (§5.2).

And from section 5.2:

The default value of a variable depends on the type of the variable and is determined as follows:

  • For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (§4.1.2).
  • For a variable of a reference-type, the default value is null.

Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference.

(As an implementation detail, there's some trickiness around the first bullet point. Although C# itself doesn't allow you to declare a parameterless constructor for value types, you can create your own parameterless constructors for value types in IL. I don't believe those constructors are called in array initialization, but they will be called in a new X() expression in C#. It's outside the realm of the C# spec though, really.)

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • 4
    When I loaded this page, it said "asked 1 minute ago", "answered 59 seconds ago". Proof that Jon Skeet is really a bot? ;) – Tim Goodman Mar 08 '13 at 17:47
  • 3
    @TimGoodman: Looking at the time-stamps, I think I answered (initially, with the brief version) 49 seconds after the question was asked. Still room for improvement. – Jon Skeet Mar 08 '13 at 17:50
  • @JonSkeet You should not respond to that critics. It's ridiculously (but funny), and in a way it's a compliment.. – Lucio Mar 08 '13 at 17:54
  • @Lucio: I didn't take it as a criticism at all :) – Jon Skeet Mar 08 '13 at 18:06
  • 1
    @Lucio Nonsense. Jon [set the bar](http://meta.stackexchange.com/a/9135) a while ago. If he can't be bothered maintain a negative 42 second delay from question to answer, then he shouldn't bother at all. Do your best or go home! – Chris Sinclair Mar 08 '13 at 18:07
  • 1
    No criticism intended, more admiration with a tinge of jealousy :) I suspected the "asked 1 minute ago" was rounded down, but the *apparent* 1 second reply was still amusing to me. – Tim Goodman Mar 08 '13 at 18:09
  • @JonSkeet: Is the default value 0 guaranteed also If I write int* p=stackalloc int[3]; ? – Destructor Feb 28 '16 at 10:22
  • @PravasiMeet: No, I don't believe so. From the C# 5 specification section 18.8: "The content of the newly allocated memory is undefined." – Jon Skeet Feb 28 '16 at 19:55
18

The default value of an automatically-initialized variable of type T, such as an array element or an instance field, is the same as the value of default(T). For reference types and pointer types, it's null. For numeric types, it is the zero of that type. For bool, it's false. For struct types, it is the struct value that has all its fields initialized to their default values.

Eric Lippert
  • 630,995
  • 172
  • 1,214
  • 2,051
5

From Arrays (C# Programming Guide):

The default values of numeric array elements are set to zero, and reference elements are set to null.

Pang
  • 9,073
  • 146
  • 84
  • 117
Douglas
  • 52,041
  • 10
  • 126
  • 179
0

Integers cannot be NULL. They will have the value '0'. Even if you try to assign NULL to an int from code, you will not be able to do it.

Pang
  • 9,073
  • 146
  • 84
  • 117
Ron
  • 836
  • 13
  • 37