C# 方法中的C参数值
如果我写a=a+115,我的消息框将返回215,但如果像a=115那样写;它将显示115 为什么呢C# 方法中的C参数值,c#,C#,如果我写a=a+115,我的消息框将返回215,但如果像a=115那样写;它将显示115 为什么呢 private void button1_Click(object sender, EventArgs e) { int c=100; MessageBox.Show(Count(c).ToString()); } public int Count(int a) { a=115; return a; } a=115将整数变量设置为115,a+=115或a=a+1
private void button1_Click(object sender, EventArgs e)
{
int c=100;
MessageBox.Show(Count(c).ToString());
}
public int Count(int a)
{
a=115;
return a;
}
a=115将整数变量设置为115,a+=115或a=a+115将在a的值上加115,然后返回该值的结果。这个问题很难解释。我想您已经尝试了两个不同版本的函数,命名为Count,如下所示 考虑此版本的函数:
public int Count1(int a)
{
a = 115;
return a;
}
无论传递什么值作为参数,它都返回115。您正在用赋值a=115覆盖参数。因此,该参数完全没有意义
现在是另一个版本:
public int Count2(int a)
{
a = a + 115;
return a;
}
此版本的函数接收变量a中的参数。然后将115加到该值上,并返回结果。所以当你这样称呼它的时候:
Count2(100)
public int Count1(int a)
{
return 115; //very clearly the input parameter is ignored
}
public int Count2(int a)
{
return a + 115; //but in this version, the input parameter is used
}
返回的值是215
如果您编写如下函数,可能更容易理解:
Count2(100)
public int Count1(int a)
{
return 115; //very clearly the input parameter is ignored
}
public int Count2(int a)
{
return a + 115; //but in this version, the input parameter is used
}
这些版本与您的版本完全相同,但我希望您会发现它们更容易理解。如果您将100传递给具有语句a=a+115(即a=100+115)的函数,那么它应该返回215
public int Count(int a)
{
a = a + 115; // a = 100 + 115
return a;
}
当您执行a=115时,它显示115,因为您正在将115分配给a。使用=,就是将右值赋给左值。当你做int c=100的时候,和你做的差不多。你把c赋值为100
当你通过c,c是100,你的公式是a=a+115,那么a就是215。当您像这样传入一个值时,a将为100。所以,当你计算a=a+115的公式时,你说的是a=100+115,你得到的是215。看起来你想知道变量a是如何翻译到你的消息框中的。这是因为变量a在count函数中。更改其值将更改count函数的结果。由于您的消息框从count中获取其值,因此您就得到了它。您的count方法只是丢弃了传递的值,并返回115。如果将a=115替换为a=a+115,并将其传递100,则返回215。这一切都是意料之中的 a=a+115意味着取a的值,加上115,然后将总数分配给a
这一切都按预期进行。是因为您认为a和c是不同的值而造成混淆吗 方法Countint a不关心值的名称是否正确 原来是c。一旦它位于该方法内部,它将被引用 只作为一个 因此,一步一步: 1将值100传递给计数方法。a=100 2计数将a的值设置为100+115。a=215 3 215返回到您的调用方法 4您的调用方法将215显示为字符串
这有帮助吗?这里有三个版本的代码来演示正在发生的事情,以及我认为您真正想问的问题: 原件:
private void button1_Click(object sender, EventArgs e)
{
// Set c to 100
int c=100;
// Print the result of Count, which (see below) is ALWAYS 115.
MessageBox.Show(Count(c).ToString());
}
public int Count(int a)
{
// Set a to 115 (there is no add here, I think this is a typo)
a=115;
// Return a, which is ALWAYS 115.
return a;
}
我认为你的意思是:
private void button1_Click(object sender, EventArgs e)
{
// Set c to 100
int c=100;
// Print the result of Count, which will be 215.
MessageBox.Show(Count(c).ToString());
}
public int Count(int a)
{
// Add 115 to a.
a+=115;
// Return the result (if a == 100, this will return 215)
return a;
}
我认为你的意思是:
private void button1_Click(object sender, EventArgs e)
{
// Set c to 100
int c=100;
// Call the count function, passing in a reference for c.
Count(ref c);
// Print the value of c. This will be 215 because the Count function set c to a new value.
MessageBox.Show(c.ToString());
}
public void Count(ref int a)
{
a+=115;
}
在最后一个例子中,我将函数更改为public void Countref int a。ref修饰符允许函数使用对另一个变量的引用来指定新值。通常,参数是按值传递的,其中传递变量值的副本
请注意,在这种情况下,第二个版本将是首选。“按引用”参数只应在确实需要时使用,而不是作为简单返回值的替代品。为什么会发生这种情况?你还不明白什么?给你的小秘密。。。100+115等于215@Benny因为你把c作为计数的第一个参数。Count的第一个参数叫做a。@DavidHeffernan:那是什么大卫?你需要大声一点,我的听力已经不是以前那么好了。我强烈建议你在提出这样的问题之前先阅读一些基本的C语言教程。第二个版本还不清楚。最好写为返回+115,而不是修改输入参数。