這是我在這裏的第一篇文章,所以大家好。我剛開始學習C#,並且在方法上沒有什麼問題。C# - 方法A的變量作爲方法B的參數
問題的關鍵 - 我必須創建程序,它首先向用戶詢問信息,然後通過另一種方法打印出來。 只有一段代碼:
namespace Uczymy3
{
class Student
{
public static void GetStudentInformation()
{
Console.WriteLine("Enter student's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter student's birth date: ");
DateTime birthDay = new DateTime();
birthDay = DateTime.Parse(Console.ReadLine());
}
public static void PrintStudentData(ref string firstName, ref DateTime birthDay)
{
Console.WriteLine("Student {0} was born in {1}", firstName, birthDay);
}
}
class Program
{
static void Main(string[] args)
{
Student.GetStudentInformation();
XXXX;
}
}
}
我的問題在XXXX開始。我想要做的是這樣的: Student.PrintStudentData(Student.GetStudentInformation()。名字,...)
但我知道這是錯誤的。 那麼,如何將一個方法的變量作爲另一個方法的參數輸入呢?
或者,也許我應該把GetStudenInformation()的結果作爲變量放在類Student的作用域中,然後將它們用作PrintStudentData中的參數?
我希望一切都清楚解釋。
創建的屬性,而不是局部變量的 –