-3

I want to clear just birkatmankontur list but this code clears all lists in my program

katmankonturlari.Add(ii, birkatmankontur);

katmankonturlari.ToList();
birkatmankontur.Clear();  
braX
  • 11,506
  • 5
  • 20
  • 33
  • `katmankonturlari.ToList();` returns a new `List` which you *discard*. You may want `katmankonturlari = katmankonturlari.ToList();` i.e. create a shallow copy of `katmankonturlari` – Dmitry Bychenko Dec 16 '19 at 07:44
  • [Reference types vs value types](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) and [how to clone a list](https://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c). – ProgrammingLlama Dec 16 '19 at 07:46
  • 2
    In your code snippet there is only _one_ list (`birkatmankontur`); what other lists are you talking about? – Bill Tür stands with Ukraine Dec 16 '19 at 07:46
  • Depending on what types those lists are, you probably have just another reference to the exact same list. So modifying one is reflected in the other as well. However with that little information it´s hard to tell exactly. – MakePeaceGreatAgain Dec 16 '19 at 07:49
  • List birkontur = new List() this is also my list and after my processes this is also clear – Betül Şen Dec 16 '19 at 07:49
  • And what is `katmankonturlari`? Btw.: is `birkontur` and `birkatmankontur` the same list? – MakePeaceGreatAgain Dec 16 '19 at 07:50
  • 1
    Welcome to Stack Overflow. Please provide a [mcve]. We don't have enough information with just those three lines of code. – Jon Skeet Dec 16 '19 at 07:51
  • `katmankonturlari.Add(ii, birkatmankontur);` is this valid syntax? – codeninja.sj Dec 16 '19 at 07:52
  • @codeninja.sj Not really. Making me wonder how the answer solved OPs problem. – MakePeaceGreatAgain Dec 16 '19 at 08:03
  • yes it is valid and solved my problem – Betül Şen Dec 16 '19 at 08:04
  • 1
    @HimBromBeere: It is if `katmankonturlari` is a dictionary. The question is still very unclear though, and the OP doesn't appear to want to try to improve it. – Jon Skeet Dec 16 '19 at 09:23
  • Most likely he's adding the list `birkatmankontur` (shape/contour?) to a list of lists in `katmankonturlari`, (list of shapes/contours? layer strokes?) and then clear the list he added preparing for a new batch. – Lasse V. Karlsen Dec 16 '19 at 09:32

1 Answers1

1

You probably want something like this:

// Create a shallow copy of birkatmankontur and add it into katmankonturlari
katmankonturlari.Add(ii, birkatmankontur.ToList());

// Clear original birkatmankontur; 
// note, that katmankonturlari contains birkatmankontur's copy  
birkatmankontur.Clear(); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215