Here the ref
in the method parameters is unneeded, in fact you don't even need parameters at all. Change it to this:
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();
}
However if it was your intention to use ref
(as an example or an exercise), then this would be how to do it. The methods now do not have a return type (void
instead of string
) because the texts are returned by means of assignment to the ref
parameters.
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.
}
For more about ref
(e.g. when or how to use it), see these questions:
- Example of practical of "ref" use
- Why use the 'ref' keyword when passing an object?