我知道默认值是C#中的ByVal。我在很多地方使用了相同的变量名称,然后我发现传递的值发生变化并返回。我想我知道C#的范围机制错了。这里公开许可证覆盖当地的许可证值。我知道我可以轻松地重命名冲突中的变量名,但我想了解有关范围的事实。C#默认传递参数是ByRef而不是ByVal
public static class LicenseWorks
{
public static void InsertLicense(License license)
{
license.registered = true;
UpdateLicense(license);
}
}
public partial class formMain : Form
{
License license;
private void btnPay_Click(object sender, EventArgs e)
{
license.registered = false;
LicenseWorks.InsertLicense(license);
bool registered = license.registered; //Returns true!
}
}
更新:我下面添加解决方案:
public static void InsertLicense(License license)
{
license = license.Clone();
...
}
行得通吧我以为它已被复制。 – 2012-03-15 09:42:53