C# 在变量中存储用户输入

C# 在变量中存储用户输入,c#,methods,C#,Methods,这是一种获取用户输入并将其存储在变量中的方法。 我应该如何创建第二个方法来访问这些变量并将它们打印到控制台? 字符串“who”用于指定此人,例如:教师、学生或家长 static void GetInformation(string who) { Console.WriteLine("Enter the {0} first name: ",who); newStudent.firstName = Console.ReadLine(); Console.WriteLin

这是一种获取用户输入并将其存储在变量中的方法。 我应该如何创建第二个方法来访问这些变量并将它们打印到控制台? 字符串“who”用于指定此人,例如:教师、学生或家长

 static void GetInformation(string who)
  {
    Console.WriteLine("Enter the {0} first name: ",who);
    newStudent.firstName = Console.ReadLine();
    Console.WriteLine("Enter the {0} last name: ",who);
    string lastName = Console.ReadLine();
    Console.WriteLine("Enter the {0} birthdate: ",who);
    string birthdate = Console.ReadLine();
    Console.WriteLine("Enter the {0} address line 1: ",who);
    string addressLine1 = Console.ReadLine();
    Console.WriteLine("Enter the {0} address line 2: ",who);
    string addressLine2 = Console.ReadLine();
    Console.WriteLine("Enter the {0} city: ",who);
    string city = Console.ReadLine();
    Console.WriteLine("Enter the {0} state: ",who);
    string state = Console.ReadLine();
    Console.WriteLine("Enter the {0} post code: ",who);
    string postCode = Console.ReadLine();
    Console.WriteLine("Enter the {0} country: ",who);
    string country = Console.ReadLine(); 
}

您可以创建一个名为
Person
的类,返回这个类的对象,其中填充了从
GetInformation
方法收集的值,并将其用作另一个方法的参数。课程示例:

public class Person
{
    public string FirstName { get; set; }
    public string LastName  { get; set; }
    public string Birthdate  { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostCode { get; set; }
    public string Country  { get; set; }
}

但我强烈建议你先读一些C#教程。这是非常基本的东西。

正如我所看到的,你是初学者,你想要的东西可以在课堂上轻松完成。然而,还有另一种方法。 在
GetInformation(string who)
的开头创建一个新列表:

List<string> list = new List<string>();
在方法的末尾,调用另一个接受
List


在该方法中,使用
foreach
并打印
列表中的元素

,因为每种类型的数据都是一个字符串,您不应该为列表而烦恼。只需做数组,这是一个基本的东西。但如果你能做到的话,将这些存储到类中是一个更好的主意。我现在正在打电话,写一些代码可能很难。使用void创建一个person类来表示所有数据,并在Main()的末尾简单地调用它。但正如我之前所说的,如果您对类还不满意,请尝试使用数组

list.Add(stringName) //instead of stringName put the variable you want to add