0

クラス内の文字列プロパティを変更しようとしたときにエラーが発生しましたが、('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
    }
4

1 に答える 1