Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a struct in an array have to be initialized?

I researched this subject but I couldn't find any duplicate. I am wondering why you can use a struct in an array without creating an instance of it.

For example, I have a class and a struct:

public class ClassAPI
{
    public Mesh mesh { get; set; }
}

public struct StructAPI
{
    public Mesh mesh { get; set; }
}

When ClassAPI is used in an array, it has to be initialized with the new keyword before being able to use its properties and methods:

ClassAPI[] cAPI = new ClassAPI[1];
cAPI[0] = new ClassAPI(); //MUST DO THIS!
cAPI[0].mesh = new Mesh();

But this is not the case with StructAPI. It looks like StructAPI doesn't have to be initialized in the array:

StructAPI[] sAPI = new StructAPI[1];
sAPI[0].mesh = new Mesh();

If you try the same thing with ClassAPI, you would get a NullReferenceException.

Why is it different with structs when using them in an array?

I understand the difference between class and struct with struct being a value type but that still doesn't make sense. To me, without the array being involved in this, it would look like I am doing this:

StructAPI sp;
sp.mesh = new Mesh();

Notice that the sp variable is not initialized and it should result in a compile-time error that says:

Error CS0165 Use of unassigned local variable 'sp'

but that's a different story when the struct is put in an array.

Is the array initializing the struct in it? I would like to know what's going on.

like image 503
Programmer Avatar asked Jul 22 '18 05:07

Programmer


People also ask

Can you initialize an array in a struct?

An array in a structure is declared with an initial size. You cannot initialize any structure element, and declaring an array size is one form of initialization.

Can you store a struct in an array?

A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure. An array within a structure is a member of the structure and can be accessed just as we access other elements of the structure.

Does an array need to be initialized?

You do not need to initialize all elements in an array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. The same applies to elements of arrays with static storage duration.

Do arrays need to be initialized C++?

Static arrays, and those declared directly in a namespace (outside any function), are always initialized. If no explicit initializer is specified, all the elements are default-initialized (with zeroes, for fundamental types).


1 Answers

As per the specification link provided by PetSerAl in the comments:

Array elements
The elements of an array come into existence when an array instance is created, and cease to exist when there are no references to that array instance.

The initial value of each of the elements of an array is the default value (Default values) of the type of the array elements.

For the purpose of definite assignment checking, an array element is considered initially assigned.

(emphasis mine).

This means that when you declare an array of T, each "cell" in the array is being initialized using default(T). For reference types default(T) returns null, but for value types default(T) returns the type default value – 0 for numbers, false for bool, and so on.

As per the Using Structs (C# Programming Guide) page:

If you instantiate a struct object using the default, parameterless constructor, all members are assigned according to their default values.

Since structs are value types, default(T) where T is a struct initializes the struct to its default value, meaning all its members will be initialized to their default values – null for reference types and whatever default value for value types.

So this line of code StructAPI[] sAPI = new StructAPI[1]; basically creates a new array of StructAPI containing a single StructAPI instance, where its mesh property is default(Mesh).

like image 81
Zohar Peled Avatar answered Oct 05 '22 04:10

Zohar Peled