2
所以后改变我显然相当新的编程,但我想弄清楚这是为什么不工作C# - 这是为什么变量没有被通过的方法被放在
我试图采取字符串myname,并将先生添加到它的开始。我知道我可以做到这一点只是myname = "Mr. " + myname
但是我想了解如何使用方法来更改变量的值。那么,为什么不改变呢?
public class Program
{
public static void Main(string[] args)
{
string myname = "harry";
Console.WriteLine(myname); //output is harry
namechanger(myname); //this should modify harry to Mr. Harry
Console.WriteLine(myname); //output is still harry?
}
static string namechanger(string name)
{
name = "Mr. " + name;
return name;
}
}
所有类型都以这种方式工作,不仅仅是原始类型...... – aquinas
也许是一种迂腐的意义,但用'class'和'struct'调用一个方法会传递非常不同的值 - 一个是指针,一个是整个结构。 “通过价值”和“通过参考”是用来区分行为差异的术语。 – Knetic
@ Knetic - 不,那是不对的。 aquinas是正确的。 – Enigmativity