修改List <t> </t>的内容时“无法修改返回值”错误

时间:2012-12-10 22:56:18

标签: c#

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

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

以下是初始化List Array

的代码
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 个答案:

答案 0 :(得分:8)

欢迎来到evil mutable structs的精彩世界!

ThreadCheck更改为class,一切正常。