如何在方法中调用方法并使用相同的变量

How can I call a method within a method and use the same variables

我是编程新手,但遇到了 C# 问题。我想创建一个控制台程序,用户在其中填写一些个人信息,然后控制台打印这些信息。我正在尝试使用 ref,但无法将用户的答案(来自方法 GetStudentInfo)与打印方法 PrintStudentDetails 联系起来。

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInfo();
            {
                string first = "";
                string last = "";
                string birthday = "";
                PrintStudentDetails(ref first, ref last, ref birthday);
            }
            GetTeacherInfo();
            GetCourseInfo();
            GetProgramInfo();
            GetDegreeInfo();
        }
        //student information
        static void GetStudentInfo()
        {
            Console.WriteLine("Enter the student's first name: ");
            string firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name: ");
            string lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthday: ");
            string birthDay = Console.ReadLine();
        }
        static void PrintStudentDetails(ref string first, ref string last, ref string birthday)
        {
            first = "test";
            last = "test";
            birthday = "test";
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }
    }
}

您需要将字符串 refs 传递给 GetStudentInfo,我认为最好使用 out 而不是 ref。
Out 与 ref 相同,只是 out 参数必须在方法 returns 之前有一个值。

static void Main(string[] args)
{
    string first;
    string last;
    string birthday;
    GetStudentInfo(out first,out last,out birthday); 
    PrintStudentDetails (first, last, birthday);    
    GetTeacherInfo();
    GetCourseInfo();
    GetProgramInfo();
    GetDegreeInfo();
}
static void GetStudentInfo(out string firstName ,out string lastName,out string birthDay)
{
    Console.WriteLine("Enter the student's first name: ");
    firstName = Console.ReadLine();
    Console.WriteLine("Enter the student's last name: ");
    lastName = Console.ReadLine();
    Console.WriteLine("Enter the student's birthday: ");
    birthDay = Console.ReadLine(); 
}
static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    Console.ReadLine();
}

https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx and https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //variables before the method that needs them, not after
            string first ="";
            string last ="";
            string birthday ="";
            GetStudentInfo(ref first, ref last, ref birthday);
            //removed extra brackets
            PrintStudentDetails(first, last, birthday);
            GetTeacherInfo();
            GetCourseInfo();
            GetProgramInfo();
            GetDegreeInfo();
        }
//student information
        //passed references in to this method
        static void GetStudentInfo(ref string firstName, ref string lastName, ref string birthday)
        {
            Console.WriteLine("Enter the student's first name: ");
            firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name: ");
            lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthday: ");
            birthday = Console.ReadLine(); 
        }
        static void PrintStudentDetails(string first, string last, string birthday)
        {
            //removed lines that reassigned variables
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }
    }
}

你最大的问题是语法,它不能像写的那样工作。 Ref 将变量地址传递给方法,基本上您需要先分配变量,然后才能传递它,一旦它被传递,方法内部变量发生的任何事情也会发生在它之外,如果这有意义的话。

如果你想在你的get info方法中调用print方法move

 PrintStudentDetails(first, last, birthday);

里面

GetStudentInfo(ref string firstName, ref string lastName, ref string birthday)
{
    //move here and change the variables too firstName, lastName, birthday, instead of first, last, birthday    
}