1

I was asked this in a C# interview how many times we can call Add or Remove method for one List instance?. My answer was int.MaxValue times but seems I was wrong.

Edit: Question was not about List size limit as pointed in comments and is in this answer. The question was how many times I can change the list.

  • Possible duplicate of https://stackoverflow.com/questions/7885294/list-size-limitation-in-c-sharp, but the tl;dr is that you could, in theory, continue to add to the list, beyond the int.MaxValue, but you'd then be unable to _address_ things beyond int.MaxValue. Hope it helps. – max Dec 16 '17 at 00:32
  • 3
    Infinite times I think. As long as the number of Add and Remove calls don't differ too much. – Wazner Dec 16 '17 at 00:33
  • @Wazner what? "As long as the number of Add and Remove calls don't differ too much."? What does exactly that mean? – Camilo Terevinto Dec 16 '17 at 00:35
  • 3
    If I call add once, then remove once you can do that forever. But if you add 2 and only remove one, you'll reach a limit at some point. – Wazner Dec 16 '17 at 00:35
  • That is like saying "as long as you don't reach the limit" stated in a very awkward way. – Camilo Terevinto Dec 16 '17 at 00:37
  • To your edit: why would you be limited as to the number of times you can call any method on `List`? That doesn't make a lot of sense – Camilo Terevinto Dec 16 '17 at 00:42
  • @CamiloTerevinto Because your computer doesn't have infinite memory. – NetMage Dec 16 '17 at 00:46
  • @NetMage Read the edit, that was not the question that Alex was asked. That was the first question which changed – Camilo Terevinto Dec 16 '17 at 00:47
  • But your comment was about "any" method, and there is a limit on calling `Add`, which is memory. – NetMage Dec 16 '17 at 00:49
  • 1
    As @Wazner said, as long as memory isn't an issue (e.g. you are adding and removing the same things, just to test the raw times you can call the methods on a list), you should be able to modify the list infinitely many times (using these two methods). But of course in practice there is the issue of memory, which case was nicely answered already. – peterxz Dec 16 '17 at 00:49

1 Answers1

4

You can change List<T> instance unlimited number times as well as other similar array-based structures in .net. Right, you can’t add more than int.MaxValue values (actually a little bit less) but you can remove and next add so many times as you wish but of cause not exceeding List<T>.Capacity value (int MaxArrayLength = 0X7FEFFFFF) and available memory.

If to look at List<T>.Add implementation you will see the code version++ which means that list instance version is changed when a new element is added. The same happens with every list modification (Add, Remove, Insert and so on). But when the version will be int.MaxValue increment operator version++ won’t throw an exception because by default overflow-checking is suppressed. Perhaps this fact moved you to int.MaxValue answer.

Also this line _items[_size++] = item is not a problem because _size is decremented when you remove items. And next line if (_size == _items.Length) EnsureCapacity(_size + 1) has no limitations too.

Vasyl Zvarydchuk
  • 3,789
  • 26
  • 37
  • 1
    You can continue to add items as long as you have memory. Read the duplicate answer – Camilo Terevinto Dec 16 '17 at 00:41
  • Right. But if you add an item and next delete it you won't change internal array length and so you can do this unlimited times. Internal array is growing only when you have not enough space. – Vasyl Zvarydchuk Dec 16 '17 at 00:46
  • Why do you say "you can’t add more than `int.MaxValue` values"? – NetMage Dec 16 '17 at 00:50
  • Next was "actually a little bit less" (int MaxArrayLength = 0X7FEFFFFF) according to List implementation source code exposed by Microsoft. – Vasyl Zvarydchuk Dec 16 '17 at 01:16
  • 1
    The problem is the question doesn't provide the full context. Do we remove just as often as we delete? Or vice versa? (Do we need to do an empty check before we attempt to remove?) Do we need to validate that the List isn't overflowing before we do another add? The question is missleading a bit. Is the list being initialized with data prior to our "adding and removing" are being called? Has the been any configuration changes to the code itself, that allows the list to be larger? is there an "artificial" limitation ? None of these things were addressed in the question. – Morten Bork Dec 17 '17 at 16:16