Demo1:加上价格标签
class Program
{
public decimal price { get; set; }
static void Main(string[] args)
{
Program p = new Program();
p.price = 25;
Console.WriteLine(p.ToString());
Console.ReadKey();
}
public override string ToString()
{
return "$"+price.ToString();
}
}
Demo2:修改时间的输出格式
public DateTime time { get; set; }
static void Main(string[] args)
{
Program p = new Program();
p.time = DateTime.Now;
Console.WriteLine(p.ToString());
Console.ReadKey();
}
public override string ToString()
{
return string.Format("{0}年{1}月{2}日",time.Year,time.Month,time.Day);
}
Demo3:
public override string ToString()
{
StringBuilder builder = new StringBuilder(16);
bool firstTime = true;
foreach (T item in this)
{
if (firstTime)
{
firstTime = false;
}
else
{
builder.AppendLine();
}
builder.Append(item);
}
return builder.ToString();
}