我正在设置多种方法,并想知道如何继续将一个变量(“top”变量)传递给不同的方法。
主要方法:
public static void Main(string[] args)
{
int[] anArray = new int[5];
int top = -1;
PushPeek(anArray);
然后我需要将顶部传递给:
public static void PushPeek(int[] ar)
{
if (ar[ar.Length -1] == ar.Length -1)
{
//do nothing
}
else
{
top = top + 1;
Console.WriteLine(ar[top]);
}
}\
我知道它与get有关;组;但我不知道怎么样,有什么帮助?
答案 0 :(得分:2)
通过引用传递:
public static void PushPeek(int[] ar, ref int top)
{
...
}
int[] anArray = new int[5];
int top = -1;
PushPeek(anArray, ref top);
答案 1 :(得分:0)