尝试更改类内的字符串属性时发生错误,但如果将('f.StartTime' 分配给字符串变量它可以工作但不会更改'StartTime' 属性)
喜欢:
string x = f.StartTime;
ChangeText(ref x); // There's no error, but f.StartTime didn't change.
//Unless:
f.StartTime = x;
当然是假方法...
所以我想执行以下代码。
public class MainClass
{
public class Foo
{
public string StartTime { get; set; }
public string ToTime { get; set; }
}
private void ChangeText(ref string Time)
{
Time = DateTime.Now.ToString();
}
public void SetClassObjects()
{
Foo f = new Foo()
{
StartTime = "Any Text",
ToTime = "Any Text"
};
ChangeText(ref f.StartTime);
// An error: A property, indexer or dynamic member access may not be passed as an out or ref parameter
}