1

I have the following c# class

public class smallclass 
{
    public Vector3 var1 = new Vector3(1,2,3);
    public string var2 = "defaultval";

    public smallclass() 
    {
      var1 = new Vector3(11,22,33);
      var2 = "constructor";
    }
}

And I have another c# class

public class bigclass  
{
    public smallclass[] smallclasses;
}

Then somewhere else in my program, I increment smallclasses size with

smallclasses = new smallclass[3];

but I realize that the three elements are null... is this correct behavior?

Allow me to clarify that I need that, whenever I resize the smallclasses array, its elements get automatically 'constructed'... I just don't know if such a thing can be done.

Differently than what I'd have expected, setting default values has no effect. Immediately after I resize the smallclasses array, the elements are null...

Any advice appreciated.

Thanks.

5
  • Perhaps your use case would call for valuetypes: they are automatically initialized at the default value, always. They also have value semantics - which is usually ok for small types Commented Oct 27, 2011 at 16:30
  • @sehe do structs instantiate with predefined values? If for example I replace 'smallclass' with the corresponding struct, would my 'bigclass.smallclasses' array get populated automatically when I resize the array, instead of giving me null values? Sorry for the ignorant questions. Commented Oct 27, 2011 at 16:35
  • @roamcel structs initialize with default values, but not user-defined default values, which you are not allowed to provide in a struct. Rather, they initialize with all fields set to zero (false for boolean fields and null for reference-type fields). Commented Oct 27, 2011 at 16:47
  • Thanks all for the great help. Commented Oct 27, 2011 at 16:52
  • structs are value type and can't be null. That's the difference. Commented Oct 27, 2011 at 19:13

4 Answers 4

2

smallclasses = new smallclass[3]; is not resizing the array. It is creating a new array.

Your array declaration (public smallclass[] smallclasses;) creates a field in the class bigclass whose initial value is null. The statement smallclasses = new smallclass[3]; creates a new array object and assigns a reference to that object to the field smallclasses.

Whenever you create a new array of a reference type (i.e., a class), all elements of the array are null. This is expected behavior. See section 1.8 of the C# specification, which says in part: "The new operator automatically initializes the elements of an array to their default value, which, for example, is zero for all numeric types and null for all reference types."

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the thorough clarification. It all makes sense now.
1

If I understand your question correctly, check out the Array.Resize method. It will help you keep the values of the objects from the original array, when the newly sized array is created. From the linked article:

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.

Update based on comment:

If you want the references in the array to refer to valid objects, you must allocate these objects and store them in the array. With a C# array you just have a reference to an array object. You still have to allocate the objects themselves and store their references into the array, if you want it to contain actual objects.

4 Comments

I'm sorry if I was unclear, but I never actually initialize the values. I just declare smallclasses, and then assign a non-zero size to the array. My problem is that the values are null in such a case, and I'd expect -and need- that they be at 'smallclass' default values.
If this is what you want, you must explicitly initialize them to newly constructed instances.
So there's no such thing as an 'autoconstructor'? Then excuse me, but what are default values for?
@roamcel the so-called default values are actually field initializers. These run when the object is constructed (with the new keyword).
1

You are asking for your array of objects to be automatically created for you. This is not how the language works. You need to construct each object individually, or in a loop.

If you do not initialize an array when you declare it, the members are initialized to the default initial value for the array type. For a reference type this is null.

2 Comments

This looks like I was trying to know: so the fact that the values are null is actually correct, expected behavior? Or I am missing something in my declaration, or class methods?
You can use array initializer syntax to initialize your array with non-null instances.
0

Try using List of Smallclass instead of the smallclass[]. You can also do Array.Resize on arrays. But the list solution is better for performance.

1 Comment

Turns out this is a good suggestion for a possible refactoring. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.