So I have a class (myClass) that I’ve created in my C# application. When working with arrays of this class, I have been thinking that it’s rather annoying having to write a loop to fill an array of myClass objects. I know that when you create an array of enum values, that array is already filled with instances of the enum which are set to their default value.
I’m wondering if the same sort of functionality can be achieved with a class so that a call like:
myClass[] myClassArray = new myClass[25];
will result in an array of myClass objects which are just instances of the empty constructor for that class.
That happens because enums are based on integer types that are value types and can’t be
null.The easiest way to initialize an array of reference types is to loop as you have described (you can write very short syntax to do so using LINQ).