How would you create a list/array containing variables instantiated from classes in C#?

58 Views Asked by At

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

2

There are 2 best solutions below

0
Ibram Reda On

make your filed to be static so you can use them in static main method

class Program
{
    static Bird bird = new Bird();
    static Bird duck = new Duck();
    static Bird crow = new Crow();
    static Bird canary = new Canary();
    static List<Bird> birds = new List<Bird>() { bird, duck, crow, canary };

    public static void Main(string[] args)
    {
        for (int i = 0; i < birds.Count; i++)
        {
            Console.WriteLine(birds[i].makeSound());
        }
    }
} 
0
Suryateja KONDLA On

you might be getting a compiler error "A field initializer cannot reference the non-static field, method, or property 'name'."

namespace ConsoleApp1;

internal class Bird
{
    public string Name { get; set; } = "Bird";

    public virtual string MakeSound()
    {
        return "Chirp";
    }
}

internal class Duck : Bird
{
    public override string MakeSound()
    {
        return "Quack";
    }
}

internal class Crow : Bird
{
    public override string MakeSound()
    {
        return "Squawk";
    }
}

internal class Canary : Bird
{
    public override string MakeSound()
    {
        return "Tweet";
    }
}

internal static class Program
{
    private static void Main(string[] args)
    {
        var bird = new Bird();
        Bird duck = new Duck();
        Bird crow = new Crow();
        Bird canary = new Canary();
        List<Bird> birdList = [bird, duck, crow, canary];

        foreach (var b in birdList)
        {
            Console.WriteLine(b.MakeSound());
        }
    }
}

If you don't want to make the classes static. then initialize them in constructor

public Program()
{
   Bird bird = new Bird();
   //Other Classes Initialize
}

or else you can make all the classes by adding static keyword

static Bird duck = new Duck();