Valerie Meunier

Valerie Meunier

  • 965
  • 693
  • 72.4k

What's best practise?

Jan 21 2023 6:47 PM

Hi

I'm wondering which way is the best: using an array of type class or using a field of type array? Here the two ways;

Thanks.

1) using an array of type class:

using System;
class Stm
{
    string name;

    static void Main(string[] args)
    {
        Stm[] myObj = new Stm[3];
        
    for (int i = 0; i < myObj.Length; i++)
            myObj[i] = new Stm();

        myObj[0].name = "Macron";
        myObj[1].name = "Biden";
        myObj[2].name = "Sunak";
    
        for (int i = 0; i < myObj.Length; i++)
            Console.WriteLine(myObj[i].naam);
    }
}
2) using a field of type array:

using System;
class Stm
{
    string[] name = new string[3];

    static void Main(string[] args)
    {
      Stmannen myObj = new Stm();
      myObj.name[0] = "Macron";
      myObj.name[1] = "Biden";
      myObj.name[2] = "Sunak";
      
      for (int i = 0; i < myObj.naam.Length; i++)
         Console.WriteLine(myObj.naam[i]);
    }
}
 


Answers (1)