C# 我的子程序赢了';I don’我不能要求我所需要的价值观
我有两个子程序,它们要求两个不同的玩家输入一个名字,但当我在主程序中调用它时,它说argument1必须用一个“ref”关键字传递,但我还没有这样做吗C# 我的子程序赢了';I don’我不能要求我所需要的价值观,c#,console-application,subroutine,C#,Console Application,Subroutine,我有两个子程序,它们要求两个不同的玩家输入一个名字,但当我在主程序中调用它时,它说argument1必须用一个“ref”关键字传递,但我还没有这样做吗 static string GetPlayer1(ref string name1) { Console.WriteLine("PlayerX enter your name:"); name1 = Console.ReadLine(); return name1; } static string GetPlayer2(
static string GetPlayer1(ref string name1)
{
Console.WriteLine("PlayerX enter your name:");
name1 = Console.ReadLine();
return name1;
}
static string GetPlayer2(ref string name2)
{
Console.WriteLine("PlayerO enter your name:");
name2 = Console.ReadLine();
return name2;
}
static void Main(string[] args)
{
string name1 = GetPlayer1(name1);
string name2 = GetPlayer2(name2);
...
事实上,你没有这样做:
string name1 = GetPlayer1(ref name1);
string name2 = GetPlayer2(ref name2);
您所做的是将ref关键字添加到方法定义中(创建ref参数)。您还必须在方法调用中使用它(创建ref参数)
在您当前的情况下,虽然不需要通过ref传递参数。您使用
GetName
方法的返回值来设置参数。实际上,您没有这样做:
string name1 = GetPlayer1(ref name1);
string name2 = GetPlayer2(ref name2);
您所做的是将ref关键字添加到方法定义中(创建ref参数)。您还必须在方法调用中使用它(创建ref参数)
在您当前的情况下,虽然不需要通过ref传递参数。但是您使用
GetName
方法的返回值来设置参数。如果使用ref参数定义方法,则需要使用参数作为引用来调用该方法:
static void Main(string[] args)
{
string name1 = GetPlayer1(ref name1);
string name2 = GetPlayer2(ref name2);
}
为了完整起见,正如其他答案已经告诉您的那样:我认为没有必要在这里调用reference,您可以在没有它的情况下实现您的目标。如果您使用ref参数定义一个方法,您需要使用该参数作为引用调用该方法:
static void Main(string[] args)
{
string name1 = GetPlayer1(ref name1);
string name2 = GetPlayer2(ref name2);
}
为了完整起见,正如其他答案已经告诉您的那样:我不认为有必要在这里调用引用,您可以在没有它的情况下实现您的目标。这里不需要方法参数中的
ref
,事实上,您甚至根本不需要参数。将其更改为:
static string GetPlayer1()
{
Console.WriteLine("PlayerX enter your name:");
return Console.ReadLine();
}
static string GetPlayer2()
{
Console.WriteLine("PlayerO enter your name:");
return Console.ReadLine();
}
static void Main(string[] args)
{
string name1 = GetPlayer1();
string name2 = GetPlayer2();
}
但是,如果您打算使用
ref
(作为示例或练习),那么这就是如何做到的。这些方法现在没有返回类型(void
而不是string
),因为文本是通过赋值给ref
参数返回的
static void GetPlayer1(ref string name1)
{
Console.WriteLine("PlayerX enter your name:");
name1 = Console.ReadLine();
}
static void GetPlayer2(ref string name2)
{
Console.WriteLine("PlayerO enter your name:");
name2 = Console.ReadLine();
}
static void Main(string[] args)
{
string name1;
string name2;
GetPlayer1(ref name1); // "ref" must now be specified, simply because
GetPlayer2(ref name2); // both methods also specify it.
}
有关
ref
(例如何时或如何使用它)的更多信息,请参见以下问题:-
-这里不需要方法参数中的
ref
,事实上,您甚至根本不需要参数。将其更改为:
static string GetPlayer1()
{
Console.WriteLine("PlayerX enter your name:");
return Console.ReadLine();
}
static string GetPlayer2()
{
Console.WriteLine("PlayerO enter your name:");
return Console.ReadLine();
}
static void Main(string[] args)
{
string name1 = GetPlayer1();
string name2 = GetPlayer2();
}
但是,如果您打算使用
ref
(作为示例或练习),那么这就是如何做到的。这些方法现在没有返回类型(void
而不是string
),因为文本是通过赋值给ref
参数返回的
static void GetPlayer1(ref string name1)
{
Console.WriteLine("PlayerX enter your name:");
name1 = Console.ReadLine();
}
static void GetPlayer2(ref string name2)
{
Console.WriteLine("PlayerO enter your name:");
name2 = Console.ReadLine();
}
static void Main(string[] args)
{
string name1;
string name2;
GetPlayer1(ref name1); // "ref" must now be specified, simply because
GetPlayer2(ref name2); // both methods also specify it.
}
有关
ref
(例如何时或如何使用它)的更多信息,请参见以下问题:-
-为什么你认为你需要向那些方法传递参数?你是否否决了两个正确的ASNWER给你?@SimonPrice:他没有,因为他不能。为什么你认为你需要向那些方法传递参数?你是否否决了两个正确的ASNWER给你?@SimonPrice:他没有,因为他不能。