class City
{
string name;
public string getName()
{
return name;
}
public void setName(String value)
{
name = value;
}
}
static void Main(string[] args)
{
City[] arr = new City[1];
arr[0].setName("New York");
}
The problem is that I get "System.NullReferenceException", "Object reference not set to an instance of an object." at the line where I set the name to New York. If I do:
City city = new City();
city.setName("New York");
I don't get any errors but I want to use an array, since I will be adding more objects. Is this possible in C# because it is in C++? Is the only way to declare 5 objects, set their names and then create an array and put them inside?