I am an absolute beginner to c# and I need some help with creating an array of classes.
class container {
public string name;
string details;
string destination;
public void get_all(){
Console.WriteLine(name + "\n" + details + "\n" + destination);
}
public void set_name() {
Console.WriteLine("enter name : ");
name = Console.ReadLine();
Console.WriteLine("enter details : ");
details = Console.ReadLine();
Console.WriteLine("enter destination: ");
destination = Console.ReadLine();
}
}
main :
class MainClass {
public static void Main(string[] args) {
for (int i=0 ; i<10 ; i++){
var cont[i] = new container();
}
cont[1].set_name();
cont[1].get_all();
}
I want to declare an array of container and set the second container of the array (as you can see in the last 3 lines) but I can't do it, can you help me?
ban? You probably just needvar cont = new container[10];before the loop. Then, you can usecont[i]inside the loop.mainclass is not full, because current code wouldn't even compile, can you post complete example. Notice thatcontarray is not defined anywhere