C# 得到;设置变量
我正在设置多个方法,并想知道如何继续将一个变量(“top”变量)传递给不同的方法 主要方法:C# 得到;设置变量,c#,variables,methods,get,C#,Variables,Methods,Get,我正在设置多个方法,并想知道如何继续将一个变量(“top”变量)传递给不同的方法 主要方法: public static void Main(string[] args) { int[] anArray = new int[5]; int top = -1; PushPeek(anArray); 然后我需要将top传递给: public static void PushPeek(int[] ar) { if (a
public static void Main(string[] args)
{
int[] anArray = new int[5];
int top = -1;
PushPeek(anArray);
然后我需要将top传递给:
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;设置但我不知道怎么做,有什么帮助吗?通过引用传递:
public static void PushPeek(int[] ar, ref int top)
{
...
}
int[] anArray = new int[5];
int top = -1;
PushPeek(anArray, ref top);
所有关于属性的信息:
自动实现的属性太棒了 这最终将是一个包含push-pop和peek内容的堆栈类,我假设顶层变量将不断变化,这不需要我使用get;?你所谓的“获取;设置”被称为属性。但在您的情况下,您只需要top是类的一个字段(即类的一个变量),但我还需要能够在不同的方法中更改它,然后返回到第一个方法,并将同一个变量与更新的变量一起使用。这行得通吗?我得把空拿出来,然后把上衣还给你吗?