返回成员函数显示字符串?C#

Dav*_*wer 0 c#

所以使用这个成员函数,我想"调用基类displayTime方法,然后将时区添加到输出消息"

 public string DisplayTime()
        {
            //return base.displayTime();
            return "okay";
        }//end of DisplayTime
Run Code Online (Sandbox Code Playgroud)

基类:

public void displayTime()
        {
            DateTime time = DateTime.Now; // Use current time
            string format = "MMM ddd d HH:mm yyyy"; // Use this format
            MessageBox.Show(time.ToString(format)); // Write to console

        }//end of displayTime
Run Code Online (Sandbox Code Playgroud)

可悲的是,我的第一个DisplayTime设置不正确,因为我不完全确定如何设置它.当谈到"return(""+ base.displayTime());"时,我尝试过不同的conbomations." 等等,这是行不通的.我不知道从哪里开始.

Vla*_*mir 7

你的displayTime()返回无效,DisplayTime()应该返回字符串.因此,如果您希望能够编写return base.displayTime();,则必须更改displayTime()为返回字符串,例如:

public string displayTime()
        {
            DateTime time = DateTime.Now; // Use current time
            string format = "MMM ddd d HH:mm yyyy"; // Use this format
            return (time.ToString(format)); // Write to console

        }//end of displayTime
Run Code Online (Sandbox Code Playgroud)