如何传递参数?

时间:2013-01-03 07:17:30

标签: c#

我正在写一个小游戏,我想从一个将它传递给其他方法的方法传递,该方法将对该数字做一些事情。这是我尝试的:

  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.

   }

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

1 个答案:

答案 0 :(得分:5)

 public static int RemoveMatches(int number )
   { 

    ///to do somthing with matchesOpeningNumber.

   }

称之为:

  int result = RemoveMatches(matchesOpeningNumber);

你应该看到: Passing Value-Type Parameters (C# Programming Guide)

如果您使用的是C#4.0或更高版本,则还可以使用Named Parameters。在这种情况下,您的电话将是:

  int result = RemoveMatches(number: matchesOpeningNumber);
                             ^^^^^^
                             parameter name