无法修改字典的返回值,因为它不是使用struct

时间:2018-03-16 23:18:24

标签: c# dictionary struct variable-assignment

我收到以下编译错误:cannot modify the return value of 'Dictionary<string, SessionLogDates>.this[string]' because it is not a variable

public static Dictionary<string, SessionLogDates> RegisteredTimes = new Dictionary<string, SessionLogDates>();

添加的键值对如下所示:

var tempDate = DateTime.Now;

RegisteredTimes.Add(requestUI, new SessionLogDates
{
    RequestStart = tempDate,
    LastProcess = tempDate
});

struct声明如下:

public struct SessionLogDates
{
    public DateTime RequestStart { get; set; }

    public DateTime LastProcess
    {
        get { return _lastProcess; }

        set
        {
            _lastProcess_cache = _lastProcess;
            _lastProcess = value;
        }
    }

    public bool LogCurrentProcess
    {
        get
        {
            var tempValue = _lastProcess_cache != _lastTimeLog;

            if (tempValue)
                _lastTimeLog = _lastProcess_cache;

            return tempValue;
        }
    }

    public bool InitialLog
    {
        get { return LastProcess == RequestStart; }
    }

    private DateTime _lastProcess { get; set; }

    private DateTime _lastProcess_cache { get; set; }

    private DateTime? _lastTimeLog { get; set; }
}

如何以这种方式重新分配?

RegisteredTimes[requestUI].LastProcess = DateTime.Now;

我是否需要更改为class而不是struct

详细原因将不胜感激。

0 个答案:

没有答案