我知道默認值是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