So, I've been trying to create a list in C# that will contain variables that are instantiations of classes. I'm still quite new to C# and this was just supposed to be a practice exercise, but lists really confuse me! I was wondering if anyone would be able to help and tell me what I'm doing wrong?
Here is my code:
using System;
using System.Collections.Generic;
class Bird
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public Bird()
{
_name = "Bird";
}
public virtual string makeSound()
{
return "Chirp";
}
}
class Duck : Bird
{
public override string makeSound()
{
return "Quack";
}
}
class Crow : Bird
{
public override string makeSound()
{
return "Squawk";
}
}
class Canary : Bird
{
public override string makeSound()
{
return "Tweet";
}
}
class Program
{
Bird bird = new Bird();
Bird duck = new Duck();
Bird crow = new Crow();
Bird canary = new Canary();
List<Bird> birdArray = new List<Bird>(){bird, duck, crow, canary};
public static void Main(string[] args)
{
for (int i=0; i < birdArray.Length; i++)
{
Console.WriteLine(birdArray[i].makeSound());
}
}
}
Thank you so so much in advance! <3
make your filed to be
staticso you can use them in static main method