Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create array of my class with default constructor?

For example

MYCLASS[] myclass = new MYCLASS[10];

Now myclass array is all null array but i want to have default constructed Array .I know that i can write loops for set default constructed but i am looking for more easy and simple way.

like image 687
Freshblood Avatar asked Jun 12 '10 09:06

Freshblood


People also ask

How do you declare an array in a constructor?

An array constructor can be used to define only an ordinary array with elements that are not a row type. An array constructor cannot be used to define an associative array or an ordinary array with elements that are a row type. Such arrays can only be constructed by assigning the individual elements.

Does the constructor automatically initialize an array?

Short answer is yes, though. :/ You're creating array of Food but the return type is Sample[] :P.

Can a constructor have an array?

Arrays can be created using a constructor with a single number parameter. An array with its length property set to that number and the array elements are empty slots.

How do you create an array in class?

Before creating an array of objects, we must create an instance of the class by using the new keyword. We can use any of the following statements to create an array of objects. Syntax: ClassName obj[]=new ClassName[array_length]; //declare and instantiate an array of objects.


6 Answers

If you don't want to write out the loop you could use Enumerable.Range instead:

MyClass[] a = Enumerable.Range(0, 10)
                        .Select(x => new MyClass())
                        .ToArray();

Note: it is considerably slower than the method you mentioned using a loop, written here for clarity:

MyClass[] a = new MyClass[10];
for (int i = 0; i < a.Length; ++i)
{
    a[i] = new MyClass();
}
like image 176
Mark Byers Avatar answered Oct 04 '22 00:10

Mark Byers


var a = Enumerable.Repeat(new MYCLASS(), 10).ToArray();
like image 43
Alexey Korovin Avatar answered Oct 04 '22 02:10

Alexey Korovin


There isn't an easier way. If you just don't like loops, you could use

MyClass[] array = new[] { new MyClass(), new MyClass(), new MyClass(), new MyClass() };

which would give you an array with 4 elements of type MyClass, constructed with the default constructor.

Otherwise, you just have the option to use a loop.

If you don't want to write that loop every time you want to construct your array, you could create a helper-method, for example as an extension method:

 static class Extension
 {
    public static void ConstructArray<T>(this T[] objArray) where T : new()
    {
        for (int i = 0; i < objArray.Length; i++)
            objArray[i] = new T();
    }
}

And then use it like this:

MyClass[] array = new MyClass[10];
array.ConstructArray();
like image 22
Maximilian Mayerl Avatar answered Oct 04 '22 01:10

Maximilian Mayerl


There isn't really a better way. You could do something like:

public static T[] CreateArray<T>(int len) where T : class, new() {
    T[] arr = new T[len];
    for(int i = 0 ; i <arr.Length ; i++) { arr[i] = new T(); }
    return arr;
}

then at least you only need:

Foo[] data = CreateArray<Foo>(150);

This approach should at least avoid any reallocations, and can use the JIT array/length optimisation. The : class is to avoid use with value-types, as with value-types already initialize in this way; just new MyValueType[200] would be better.

like image 40
Marc Gravell Avatar answered Oct 04 '22 01:10

Marc Gravell


You can use LINQ.

var a = (from x in Enumerable.Range(10) select new MyClass()).ToArray();
like image 30
TcKs Avatar answered Oct 04 '22 00:10

TcKs


If we want to do all job in only one line code so this is best

MYCLASS[] myclass = (new MYCLASS[10]).Select(x => new MYCLASS()).ToArray();
like image 33
Freshblood Avatar answered Oct 04 '22 00:10

Freshblood