So I am learning how instances work and how to set them and such and I am wondering if I can output the variables of an instance without to output them separately.
Ex//
class Program
{
static void Main(string[] args)
{
Person Ryan = new Person();
Ryan.Age = 16;
Ryan.Name = "Rynoh97";
}
}
class Person
{
public int Age = 0;
public string Name = "";
}
Now to output my age I need to do Console.WriteLine(Ryan.Age);
and for my name I need Ryan.Name
but is there a way to output them both at the same time without making something complex.
I've tried Ryan.ToString()
but I get the location of the class for Person
.
Any advice?
You can override ToString in the Person class and make it output however you want it to when you call ToString.
There's an example here: