c#的一个关于数据结构的问题

来源:百度知道 编辑:UC知道 时间:2024/09/20 03:01:07
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");

int ss = 11;
NewMethod(ss);
Console.WriteLine(ss);
}

private static int NewMethod(int p)
{
string aa="a";
int s=11;
p++;
return p;
}
}
}

问一下为什么ss还是11,怎么样才可以让它变++,这是什么原理呢

因为 int 是属于值传递,也就是说,它是直接传值过去的,并不是传引用。

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");

int ss = 11;
NewMethod( ref ss );
Console.WriteLine(ss);
}

private static int NewMethod( ref int p)
{
string aa="a";
int s=11;
p++;
return p;
}
}
}

这样就可以了。

只是值传递而已。

ss=NewMethod(ss);
Console.WriteLine(ss);