2

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.

4
  • Possible duplicate of: stackoverflow.com/questions/583574/… Commented Jan 21, 2012 at 21:03
  • The reason why all fields in your array will initialize with null is that classes are reference types. Enums and primitves are value types and therefore able to initialize with a default value. Commented Jan 21, 2012 at 21:03
  • Not really a duplicate; this question is a "How to" question, while the other one is a "Why" question about language behavior. Commented Jan 21, 2012 at 21:10
  • I was pretty sure I wouldn't be able to do what I wanted because the classes aren't value types as Oded points out. Just wanted to see if there was something cute I could do to avoid writing the loops (or putting them in a method). Using Linq is a decent idea, but the point was the ease of creation and if I'm writing a few extra lines, I might as well write my loop. Commented Jan 21, 2012 at 21:14

3 Answers 3

8

Try this:

MyClass[] myClassArray = Enumerable
    .Range(0, 25)
    .Select(i => new MyClass())
    .ToArray();

This will create 25 instances of your class, and put them into an array.

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

2 Comments

You have not declared a loop, but a loop is still happening.
@Oded Absolutely, there is no way to avoid the loop. But you can at least get it out of your sight.
2

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.

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).

2 Comments

This what I was afraid of. I just wasn't sure if I could overload something in my class to make things easier.
@marrithl2 - As you can see from other answers, you can write: 1. A method to do this. 2. An extension method. 3. A LINQ query to do this. Or, 4. As Henk posted - convert the class to a struct if possible.
2

Why not just encapsulate the annoying loop in a method?

public T[] CreateInstances<T>(int arrayLength) where T: new()
{
    T[] result = new T[arrayLength];
    for (int i=0; i<arrayLength; i++)
       result[i] = new T();
    return result;
}

1 Comment

Beat me to it. I think that method should be static though... and it misses a return result; ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.