How can I display the arguments of my objects in the cout method?

class Program { static void Main(string[] args) { Instr box_1 = new Instr(15,13,"tree"); Instr box_2 = new Instr(53,35,"metal"); cout(box_1); cout(box_2); Console.ReadKey(); } public static void cout(object g) { } } 
  • @FoggyFinder sorry, I don't understand - ZOOM SMASH
  • @FoggyFinder need to output 2 numbers and the string that the object contains - ZOOM SMASH
  • 2
    Possible duplicate question: listBox. How to display normal values? - Grundy
  • @FoggyFinder I thank - ZOOM SMASH

1 answer 1

 public static void cout(object g) { //нужно привести параметр к нужному вам типу Instr box = g as Instr; //если получилось, то вывести нужные значения if (box != null) { Console.WriteLine($"{box.НужноеСвойство}"); } } 

Or redefine the ToString() method in your Instr class. To do this, write override press the spacebar - the studio will list the available methods for redefining the methods, select ToString() . And then in it write something like

 return $"{this.НужноеСвойство1} - {this.НужноеСвойство2}, {this.НужноеСвойство3}"; 

then you can call ToString() in your public static void cout(object g) method and print the values ​​of all the properties you need.