0

我正在编写一个小游戏,我想从一个将其传递给其他方法的方法传递,该方法将对该数字执行某些操作。这是我尝试的:

  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.

   }

当我尝试将其传递给第二种方法时,它失败了.. :( 为什么会这样?

4

1 回答 1

5
 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
于 2013-01-03T07:18:58.090 回答