-2

How can I initialize array for this generic class please help

 public class MarvellousArray<T>
 {
    private T[] array;

    public MarvellousArray(int size)
    {
        array = new T[size];
    }
    public void Accept()
    {

        var i = 0;
        for (i = 0; i < array.Length; i++)
        {

        }
    }
    public void Display()
    {

    } 
}
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
  • What exactly are you trying to achieve? – Rui Jarimba Oct 20 '18 at 10:05
  • I want to initialize the array using for loop – rahul shinde Oct 20 '18 at 10:06
  • In the loop just assign the elements: `array[i] = MakeTheValue(…)` – Richard Oct 20 '18 at 10:13
  • Also a bit puzzled about what the use for this might be, but this code does not have any compile time issues. I've just tried it by making an instance: `[TestMethod] public void Aaa() { var ma = new MarvellousArray(3); ma.Accept(); }` and using it by inserting this into the loop's body: `Debug.Print(array[i].ToString());` No issues! – DanDan Oct 20 '18 at 10:22
  • Nothing is Working – rahul shinde Oct 20 '18 at 10:40
  • array[i] = Convert.ToInt32(Console.ReadLine()); This syntax is giving me error – rahul shinde Oct 20 '18 at 10:42
  • how should the compiler know that at runtime you instantiated an array of int? for the compiler it is an array of T, you cannot just assign an int to T when T could be anything. – DanDan Oct 20 '18 at 10:52

2 Answers2

1

If i understand what you mean... You could do this if you need to new T

public class MarvellousArray<T>
   where T : new()
{
   private readonly T[] _array;

   public MarvellousArray(int size)
   {
      _array = new T[size];

      for (var i = 0; i < size; i++)
         _array[i] = new T();

   }

   ...

or just default which will be null for reference types

public class MarvellousArray<T>
{
   private readonly T[] _array;

   public MarvellousArray(int size)
   {
      _array = new T[size];

      for (var i = 0; i < size; i++)
         _array[i] = default(T);

   }

   ...
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • However, initializing an array with its element‘s default value seems futile to me as this what arrays are already initialized to begin with. – ckuri Oct 20 '18 at 11:29
0

If you want to initialize any array, there is the whole array Initilazier Syntax: All possible array initialization syntaxes

The question is really what you want to Initialize it with.

The default value? ckuri pointed out that in most cases Array are initialied with that already.

A Specific value?

public static void setAll(int[] array, int value){
  for(int i = 0; i<array.Lenght;i++)
    array[i]=value;
}

I wrote this from memory. I am unsure if it was Size or Lenght with arrays. You could improve it with stuff like generics. Or a integer variable on how many times you want the value written.

Do you want them of a minimim size? Use List[T] and just define the minimum size with the constructor overload.

The values of another Array? List.AddRange is there for that.

Do you look for Lazy Initializsation? Then look at Lazy[T]. Alterantively you could write your class Array accessors. And initialize a empte value before you hand it out. Same way people do it with properties.

Christopher
  • 9,634
  • 2
  • 17
  • 31