有人可以为我解释一下,这甚至是如何工作的?
ToString()
怎么可能在不创建实际方法的情况下从 中返回任何内容?
using System;
namespace ConsoleApplication4
{
class Person
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
**/*
* What is ToString() method?
* What I am overriding Here?
*/**
public override string ToString()
{
return "Name= " + Name + " Age=" + Age;
}
}
class Sample
{
static void Main(string[] args)
{
Person P1 = new Person();
P1.Name = "ABC";
P1.Age = 21;
Console.WriteLine(P1.ToString());
Console.ReadLine();
**//Ouput Name = ABC Age = 23**
}
}
}