C# 如何使用此关键字从方法中获取数据

C# 如何使用此关键字从方法中获取数据,c#,C#,Messages类中有一个名为questionWhatisYourName()的方法,该方法由另一个名为QuestionName()的方法调用。该方法由类程序的主方法调用。 是否可以将数据从问题WhatisYourName()返回到问题姓氏(),而不在messages类中调用它?也许我应该把它作为一个参数传递给question姓氏()方法 using System; using System.Collections.Generic; using System.Linq; using System

Messages类中有一个名为
questionWhatisYourName()
的方法,该方法由另一个名为
QuestionName()的方法调用。
该方法由类程序的主方法调用。 是否可以将数据从
问题WhatisYourName()
返回到
问题姓氏()
,而不在messages类中调用它?也许我应该把它作为一个参数传递给
question姓氏()
方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class TextEffects
{
    public void drawLine() {
        Console.WriteLine("------------------------------------");
    }
    public void drawLine(String linetype)
    {
        Console.WriteLine(linetype +
        linetype + linetype + linetype + linetype + linetype + linetype + linetype + linetype + linetype
        + linetype + linetype + linetype + linetype + linetype + linetype + linetype + linetype);
    }

}
class Messages
{
    public void hello()
    {
        Console.WriteLine("Hello and welcome.");
    }
    public String questionWhatIsYourName()
    {
        Console.WriteLine("What is your name?");
        String name = Console.ReadLine();
        return name;
    }
    public void questionSurname()
    {
        String myname = this.questionWhatIsYourName();
        Console.WriteLine(myname + " what is your surname?");

    }
}
class Program
{
    static void Main(string[] args)
    {
        TextEffects texteffects = new TextEffects();
        Messages messages = new Messages();
        texteffects.drawLine("+");
        messages.hello();
        messages.questionSurname();
    }
}
}

是,将其作为字符串参数传入

public void questionSurname(string myname)
{
    // ...
}
可以这样称呼:

string name = messages.questionWhatIsYourName();
messages.questionSurname(name);

是,将其作为字符串参数传入

public void questionSurname(string myname)
{
    // ...
}
可以这样称呼:

string name = messages.questionWhatIsYourName();
messages.questionSurname(name);

只需将名称添加到类属性中,并按如下方式使用:

 class Messages
 {
     private string myName;
     public void Hello()
     {
         Console.WriteLine("Hello and welcome.");
     }
     public void QuestionWhatIsYourName()
     {
        Console.WriteLine("What is your name?");
        myName = Console.ReadLine();            
     }
     public void QuestionSurname()
     {
         QuestionWhatIsYourName();
         Console.WriteLine(myname + " what is your surname?");
     } 
}
请注意,您的代码不是statndard。方法应从每个案例开始
而类消息的名称并不描述该类负责的内容。

只需将名称添加到类属性中,并像这样使用它:

 class Messages
 {
     private string myName;
     public void Hello()
     {
         Console.WriteLine("Hello and welcome.");
     }
     public void QuestionWhatIsYourName()
     {
        Console.WriteLine("What is your name?");
        myName = Console.ReadLine();            
     }
     public void QuestionSurname()
     {
         QuestionWhatIsYourName();
         Console.WriteLine(myname + " what is your surname?");
     } 
}
请注意,您的代码不是statndard。方法应从每个案例开始 类消息的名称并不能描述该类负责什么