C#重写ToString()

C#中所有的类均继承Object,同样继承了Object中的ToString(),直接调用将会返回类名,往往使用中我们希望它按照我们的想法转换成字符串,这就需要重写ToString()

Object中定义如下:

        //
        // 摘要:
        //     Returns a string that represents the current object.
        //
        // 返回结果:
        //     A string that represents the current object.
        public virtual string? ToString();

重写:

public override string ToString()
        {
            return "Hello";
        }


版权声明:本文为shaynerain原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。