2011-04-06 75 views
2

我设置了多种方法,不知道如何继续一个变量(“顶”变量)传递给不同的方法。获取;组;变量

主要方法:

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]); 
     } 
    }\ 

我知道这涉及到与得到的东西;组;但我不知道如何,有什么帮助?

回答

2

传递它的参考:

public static void PushPeek(int[] ar, ref int top) 
{ 
    ... 
} 

int[] anArray = new int[5]; 
int top = -1; 
PushPeek(anArray, ref top); 
+0

这最终将与推动流行音乐和偷看的东西堆类,我认为上面的变量将始终如一地发生变化,也不会要求我使用get;? – Madox 2011-04-06 22:31:26

+0

你叫什么?“获得;设置;”被称为财产。但在你的情况下,你只需要上面是你的类的字段(即类的变量) – 2011-04-06 22:45:32

+0

对,但我也需要能够改变它在不同的方法,然后再回到第一种方法,并使用与更新的变量相同的变量。这会工作吗? – Madox 2011-04-06 22:50:27