C# - 从包含原始副本的数组中删除一个对象

发布于 2025-02-11 03:21:36 字数 879 浏览 1 评论 0原文

我在以下结构中持有一个阵列 列表[] Pagestore =新列表[2]; 数组的每个单元格将包含页面对象列表。

我编写了一个在循环中运行的函数,并且在每次迭代中都创建了一个新的页面列表。 我将列表保留在上面的列表中。 在每次迭代之前,我删除页面的内容,并在数组本身上完成删除。

如何防止这一点,以使阵列保存我保存的记录?

我的代码:

List<Page>[] pageStore = new List<Page>[2];
public void LoadExcel(Dictionary<string,string[]> FilePaths){
  List<Page> pages = new List<Page>();
  int pageStoreIndex = 0;
  foreach (KeyValuePair<string, string[]> entry in 
  FilePaths) {
   pages.Clear();
   for (int i = 0; i < entry.Value.Length; i++) {
     if (i == 0)
     pages = fileManager.ParseExcelToObjects(excelDataSet, 
ProjectTypeSelected.Name,entry.Key.Equals(Enum.Enums.ConnectorSide.COMBINED.ToString()) ? false : true);
    ...
    ...
   }
   if (pages.Count > 0) 
     pageStore[pageStoreIndex++] = pages;
  }
}

page.clear()也清除了Pagestore。

I hold an array in the following structure
List[] pageStore = new List[2];
Each cell of the array will contain a list of Page objects.

I wrote a function that ran in a loop and in each iteration creates a new list of Page.
I keep the list created in the above List.
Before each iteration I delete the contents of pages and the deletion is also done on the array itself.

How can this be prevented so that the array will keep the records I saved?

my code :

List<Page>[] pageStore = new List<Page>[2];
public void LoadExcel(Dictionary<string,string[]> FilePaths){
  List<Page> pages = new List<Page>();
  int pageStoreIndex = 0;
  foreach (KeyValuePair<string, string[]> entry in 
  FilePaths) {
   pages.Clear();
   for (int i = 0; i < entry.Value.Length; i++) {
     if (i == 0)
     pages = fileManager.ParseExcelToObjects(excelDataSet, 
ProjectTypeSelected.Name,entry.Key.Equals(Enum.Enums.ConnectorSide.COMBINED.ToString()) ? false : true);
    ...
    ...
   }
   if (pages.Count > 0) 
     pageStore[pageStoreIndex++] = pages;
  }
}

page.Clear() cleared pageStore also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2025-02-18 03:21:36

您需要创建列表的副本。

pageStore[pageStoreIndex++] = new List<Page>(pages);

You need to create a copy of the list.

pageStore[pageStoreIndex++] = new List<Page>(pages);
悲歌长辞 2025-02-18 03:21:36

像大多数.NET对象一样,list&lt; t&gt;是一种参考类型。您的行:

pageStore[pageStoreIndex++] = pages;

只是存储对单页实例的引用。它不会复制跨越值。每个数组条目都指向同一列表。

正如其他答案所说,如果要保留单独的列表,则需要每次创建一个新列表:

List<Page>[] pageStore = new List<Page>[2];

public void LoadExcel(Dictionary<string,string[]> FilePaths)
{
  int pageStoreIndex = 0;

  foreach (KeyValuePair<string, string[]> entry in FilePaths) 
  {
    //create a new list for each loop iteration
    var pages = new List<Page>();

    for (int i = 0; i < entry.Value.Length; i++) 
    {
      //ps the following lines don't make sense. You've created a new List above, 
      //but ParseExcelToObjects() ignores it and returns its own list.
      //So either your new List<Page>() is pointless, or this call is different in the real code.
      if (i == 0)
        pages = fileManager.ParseExcelToObjects(excelDataSet, ProjectTypeSelected.Name,entry.Key.EqualsEnum.Enums.ConnectorSide.COMBINED.ToString()) ? false : true);
      ...
      ...
    }

    if (pages.Count > 0) 
      pageStore[pageStoreIndex++] = pages;
  }
}

Like most .Net objects, List<T> is a reference type. Your line:

pageStore[pageStoreIndex++] = pages;

is just storing a reference to the single pages instance. It's not copying the values across. Every array entry is pointing at the same list.

As other answers have said, you need to create a new list each time if you want to keep separate lists:

List<Page>[] pageStore = new List<Page>[2];

public void LoadExcel(Dictionary<string,string[]> FilePaths)
{
  int pageStoreIndex = 0;

  foreach (KeyValuePair<string, string[]> entry in FilePaths) 
  {
    //create a new list for each loop iteration
    var pages = new List<Page>();

    for (int i = 0; i < entry.Value.Length; i++) 
    {
      //ps the following lines don't make sense. You've created a new List above, 
      //but ParseExcelToObjects() ignores it and returns its own list.
      //So either your new List<Page>() is pointless, or this call is different in the real code.
      if (i == 0)
        pages = fileManager.ParseExcelToObjects(excelDataSet, ProjectTypeSelected.Name,entry.Key.EqualsEnum.Enums.ConnectorSide.COMBINED.ToString()) ? false : true);
      ...
      ...
    }

    if (pages.Count > 0) 
      pageStore[pageStoreIndex++] = pages;
  }
}
夜唯美灬不弃 2025-02-18 03:21:36

您已将Pagestore中的值分配为页面对象。您需要使用list&lt; t&gt;的复制构造函数创建一个新对象:

if (pages.Count > 0) 
     pageStore[pageStoreIndex++] = new List<Page>(pages);

You've assigned the value in pageStore to be the pages object. You'll need to create a new object using List<T>'s copy constructor:

if (pages.Count > 0) 
     pageStore[pageStoreIndex++] = new List<Page>(pages);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文