Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 83k

problem with instantiating object of class using array

Sep 5 2021 5:05 PM

Hi

This gives an error at line  myObj[0].Name = "Macron"; 

Thanks.

See my code:

using System;
class Friends
{
    public string Name;
    static void Main(string[] args)
    {
        Friends[] myObj = new Friends[3];
        myObj[0].Name = "Macron";
        myObj[1].Name = "Rutte";
        myObj[2].Name = "Johnson";
        Console.WriteLine(myObj[0].Name);
        Console.WriteLine(myObj[1].Name);
        Console.WriteLine(myObj[2].Name);
    }
}


Answers (2)