zoukankan      html  css  js  c++  java
  • C# 如何重写ToString方法

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

    高山仰止, 景行行止。 四牡鲱鲱, 六辔如琴。 觏尔新婚, 以慰我心。
  • 相关阅读:
    linux下安装mysql
    python -- 相对路径、绝对路径、以及路径的获取
    Jekyll 使用入门
    argparse 使用指南
    requests快速入门
    Pandoc中的Markdown语法
    利用Github Pages建立仓库“门面”
    Anaconda使用入门
    Python连接SQL Server数据库
    SQL Server 部署CLR程序集错误`6218`
  • 原文地址:https://www.cnblogs.com/davidshi/p/3291646.html
Copyright © 2011-2022 走看看