In C# resizing an array (increasing its size in this case) initializes the new segment with default values – is this reliable?

Yes, you can rely on that. From the documentation (emphasis mine):

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one. array must be a one-dimensional array.

Allocating a new array is guaranteed to populate it with default values (effectively "set all bits to 0"), so if we trust the description, the result of the overall Array.Resize operation would indeed have default values for all elements which haven't been copied from the old array.


Yes, it is reliable. One way of looking at it - if the new array elements didn't contain the default value, what would they contain? The method isn't going to make up values.

Not that I normally write unit tests for framework code, but it's an easy way to test expected behavior, especially if the documentation leaves us uncertain.

[TestMethod]
public void Resizing_array_appends_default_values()
{
    var dates = new DateTime[] {DateTime.Now};
    Array.Resize(ref dates, dates.Length + 1);
    Assert.AreEqual(dates.Last(), default(DateTime));

    var strings = new string[] { "x" };
    Array.Resize(ref strings, strings.Length + 1);
    Assert.IsNull(strings.Last());

    var objects = new object[] { 1, "x" };
    Array.Resize(ref objects, objects.Length + 1);
    Assert.IsNull(objects.Last());
}

It goes without saying that I would discard this unit test after running it. I wouldn't commit it.