我正在使用自动实现的属性.我想解决以下问题的最快方法是声明我自己的支持变量?
public Point Origin { get; set; }
Origin.X = 10; // fails with CS1612
Run Code Online (Sandbox Code Playgroud)
错误消息:无法修改'expression'的返回值,因为它不是变量
尝试修改作为中间表达式结果的值类型.由于该值未持久存在,因此该值将保持不变.
要解决此错误,请将表达式的结果存储在中间值中,或使用中间表达式的引用类型.
我试图了解如何通过"引用"分配给c#中的类字段.
我有以下示例要考虑:
public class X
{
public X()
{
string example = "X";
new Y( ref example );
new Z( ref example );
System.Diagnostics.Debug.WriteLine( example );
}
}
public class Y
{
public Y( ref string example )
{
example += " (Updated By Y)";
}
}
public class Z
{
private string _Example;
public Z( ref string example )
{
this._Example = example;
this._Example += " (Updated By Z)";
}
}
var x = new X();
Run Code Online (Sandbox Code Playgroud)
运行上面的代码时,输出是:
X(由Y更新)
并不是: …