如何传递参数
本文关键字:参数 何传递 | 更新日期: 2023-09-27 17:50:42
我正在写一个小游戏,我想从一个方法,将它传递给其他方法,将做一些与该数字。下面是我的尝试:
public static int MatchesStart()
{
Console.Write("how many matches do you want to play with?");
string matchesStartingNumber = Console.ReadLine();
int matchesOpeningNumber = Convert.ToInt32(matchesStartingNumber);
for (int i = 0; i < matchesOpeningNumber; i++)
{
Console.Write("|");
}
return matchesOpeningNumber;
}
public static int RemoveMatches( *** i want here: matchesOpeningNumber )
{
///to do somthing with matchesOpeningNumber.
}
当我试图将传递给第二个方法时,它失败了。为什么呢?
public static int RemoveMatches(int number )
{
///to do somthing with matchesOpeningNumber.
}
这样写:
int result = RemoveMatches(matchesOpeningNumber);
你应该看到:传递值型参数(c#编程指南)
如果你使用c# 4.0或更高版本,你也可以使用命名参数。在这种情况下,您的呼叫将是:
int result = RemoveMatches(number: matchesOpeningNumber);
^^^^^^
parameter name