1

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?

5
  • 1
    Try: Console.WriteLine(carcollection[0].Label); or other properties Commented Feb 15, 2022 at 12:05
  • oh my god... i am so stupid. Thanks. Thats it :D Commented Feb 15, 2022 at 12:06
  • 3
    @Ceraxes You can also override the ToString() method definition on the Car class if you own/control that. Commented Feb 15, 2022 at 12:07
  • @MathiasR.Jessen I also heard about that. How would i use it correctly? is it Console.WriteLine(carcollection[0].ToString); Commented Feb 15, 2022 at 12:17
  • No, Console.WriteLine will automatically call ToString() for you, so as soon as you define eg. public override string ToString() { return string.Join(" ", Label, Name); } (or similar) on the Car class, Console.WriteLine(carcollection[0]) will automatically produce meaningful results. Commented Feb 15, 2022 at 12:25

4 Answers 4

3

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 }

Sign up to request clarification or add additional context in comments.

Comments

1

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());

Comments

1

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.

Comments

1

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 type name of your class as string object.

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]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.