Home > Enterprise >  How to get an output of an object stored in an array in C#
How to get an output of an object stored in an array in C#

Time:02-15

I am stuck at a really stupid mistake. I want to create an object of the Class Car and store the objects in an array. So far so good. But I cant figure out how, or better to say why I cant get any output which makes sence with Console.WriteLine();

I am pretty sure the solution is simple as always, but I cant figure it out on my own.

This is my code

 static void Main(string[] args)
    {
        Car audi = new Car("Audi", "A4", 4);                //string Lable, string name, int tires
        Car vw = new Car("VW", "Golf", 4);

        

        Car[] carcollection = new Car[] {audi,vw};

        Console.WriteLine(carcollection[0]);


    }

So is there a way that i can write all the values of an object with Console.WriteLine() without typing in audi.Name, audi.Lable etc?

CodePudding user response:

So you would define the class Car as follows: public class Gar

 {
        public string name;
        public string label;

        public string GetInfoOfCar() 
        {
            return this.name   "   "   this.label;
        }
    }

Then the "GetInfoOfCar" method will return the desired information. To extract object information, you can do the following:

Console.WriteLine(carcollection[0].GetInfoOfCar());

CodePudding user response:

If using record is allowed. Try this: (.NET 5 )

public record Car(string Name, string model, int doors);

Then in your class:

public class MyClass {
    public void ShowCarInfo() {
        Car audi = new Car("Audi", "A4", 4);
        Car vw = new Car("VW", "Golf", 4);
        Car[] carcollection = new Car[] {audi,vw};
        Console.WriteLine(carcollection[0]);
    }
}

Output: Car { Name = Audi, model = A4, doors = 4 }

CodePudding user response:

As mentioned in comments, you can solve this by overriding Car.ToString():

public class Car
{
    public string Name;
    public string Label;
    public int TireCount;

    public Car(string label, string name, int tires)
    {
        Name = name; Label = label; TireCount = tires;
    }

    public override string ToString()
    {
        return $"{Label} {Name}, ({TireCount} tire{ (TireCount == 1 ? string.Empty : "s") })";
    }
}

Now, whenever you call Console.WriteLine(carcollection[0]), Console.WriteLine will automatically call ToString() on the car and print Audi A4, (4 tires) for example.

CodePudding user response:

Console.WriteLine requires an string input to correctly print result, so it converts your Car object to string by calling default ToString method. The default behaviour for this method is to provide full string type name of your class.

But you can override it in your class like this, or use Record type example of which was provided in other answers.

class Car
{
    public string Label { get; set; }
    public string Name { get; set; }
    public long Tires { get; set; }

    public Car(string label, string name, long tires)
    {
        Label = label;
        Name = name;
        Tires = tires;
    }

    public override string ToString()
    {
        return $"Brand: {Label}, Label: {Name}, Tires: {Tires}";
    }
}

class Program
{
    static async Task Main(string[] args)
    {
        Car audi = new Car("Audi", "A4", 4); //string Lable, string name, int tires
        Car vw = new Car("VW", "Golf", 4);


        Car[] carcollection = new Car[] { audi, vw };

        Console.WriteLine(carcollection[0]);
    }
}
  • Related