Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with removing elements in a list of list in C#
    primarykey
    data
    text
    <p>I have a program where I have three classes A, B and C. A contains a member that is a list of B, and B consists of a member that is a list of C. A contains a method that is called RemoveB(), and similarly B contains a method that is called RemoveC(). RemoveB() and RemoveC() remove the first element of the BList and the CList respectively.</p> <p>The following is what it all looks like:</p> <pre><code>class A { ... List&lt;B&gt; BList; ... public void RemoveB() { this.BList.Removeat(0) } } class B { ... List&lt;C&gt; CList; ... public void RemoveC() { this.CList.Removeat(0) } } class Trial { ... } </code></pre> <p>In my main function I first populate BList and the CList in each of these BList elements. There are N elements in BList and each of the BList elements contains M elements in CList. Then I do the following.</p> <pre><code>A aObject = new A(); // Populates BList and CList. for(int i = 0; i &lt; N; i++) { Console.WriteLine("BList: {0} ({1})", i, aObject.BList[0].Count); for (int j = 0; j &lt; M; j++) { Console.WriteLine("CList: {0}", j); aObject.BList[0].RemoveC(); } aObject.RemoveB(); } </code></pre> <p>When I do this, I get the following problem. When I try to remove elements in CList belonging to element BList[0], the corresponding elements in CList in BList[1], BList[2]... BList[N-1] are also removed. The following is the output I see from the running the program.</p> <pre><code>BList 0 (M) CList 0 CList 1 CList 2 . . . CList M-1 BList 1 (0) BList 2 (0) BList 3 (0) . . . BList N-1 (0) </code></pre> <p>Could someone tell me why this could be happening? Am I doing some fundamental mistake somewhere.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload