C#重写tostring方法

时间:2025-11-18 20:54:04

1、重写tostring()方法,第一在需要重写tostring()方法的类中添加tostring方法,

代码如图所示:

C#重写tostring方法

2、调用重写tostring()方法,代码如图所示:

C#重写tostring方法

3、所有的代码如下:

using System;

namespace HelloWorldApplication

{

   class HelloWorld

   {

      static void Main(string[] args)

      {

         /* 我的第一个 C# 程序*/

         Console.WriteLine("Hello World");

/*调用重写的方法*/

this.tostring();

         Console.ReadKey();

      }

   }

/*重写tostring()方法*/

public void tostring(){

Console.WriteLine("您好,世界");

}

}

C#重写tostring方法

4、输出:

Hello World

您好,世界!

C#重写tostring方法

© 2025 光影知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com