我目前正在处理一个 C# 项目,我需要将数据添加到自定义列表数组中。然后我稍后需要枚举列表,找到特定记录然后修改数据。但是,我在 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;
}
}
}
}
如何在不出现上述错误的情况下修改列表数组的内容?