修改 List<T> 内容时出现“无法修改返回值”错误。

4

我目前正在进行一个C#项目,需要将数据添加到自定义的List数组中。然后稍后需要枚举列表,找到特定的记录,然后修改数据。但是,在Visual Studio中出现了错误。

Cannot modify the return value of System.Collections.Generic.List<EmailServer.ThreadCheck>.this[int] because it is not a variable. 

以下是初始化列表数组的代码:
static List<ThreadCheck> threadKick = new List<ThreadCheck>();

以下是我向List数组添加数据的方法。
public static void addThread(ILibraryInterface library, int threadID, string threadName)
{
    if (Watchdog.library == null)
    {
        Watchdog.library = library;
    }
    long currentEpochTime = library.convertDateTimeToEpoch(DateTime.Now);
    threadKick.Add(new ThreadCheck()
    {
        threadName = threadName,
        threadID = threadID,
        epochTimeStamp = currentEpochTime
    });
    library.logging(classDetails + MethodInfo.GetCurrentMethod().Name, string.Format("Thread ID: {0} has been added to watchdog for {1}",
        threadID, threadName));
}

以下是我尝试修改列表中数据的代码。
public static void kickThread(int threadId)
{
    lock (threadKick)
    {
        for (int i = 0; i < threadKick.Count; i++)
        {
            if (threadKick[i].threadID == threadId)
            {
                threadKick[i].epochTimeStamp = library.convertDateTimeToEpoch(DateTime.Now);
                break;
            }
        }
    }
}

我该如何修改列表数组的内容,而不会出现上述错误?
1个回答

9

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接