-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List<T>.AddRange with erroneous input has inconsistent behavior #76116
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsDescriptionOut of curiosity I experimented with the Reproduction StepsHere is my custom class MyCollection<T> : ICollection<T>
{
public int Count => Int32.MaxValue;
public void CopyTo(T[] array, int arrayIndex) { } // Do nothing
public bool IsReadOnly => throw new NotImplementedException();
public void Add(T item) => throw new NotImplementedException();
public bool Remove(T item) => throw new NotImplementedException();
public void Clear() => throw new NotImplementedException();
public bool Contains(T item) => throw new NotImplementedException();
public IEnumerator<T> GetEnumerator() => throw new NotImplementedException();
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
} Passing an instance to new List<int>().AddRange(new MyCollection<int>()); Output:
But if the list is not empty before the List<int> list = new();
list.Add(1);
list.AddRange(new MyCollection<int>());
Console.WriteLine($"list.Count: {list.Count}"); Output:
This is not a realistic scenario, since the
|
Most BCL optimizations using runtime/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs Line 257 in 315bdd4
Would you be interested in contributing a PR? |
@eiriktsarpalis yea I don't think that it needs fixing either. Adding an arithmetic check that will slow down everyone's code, just for the sake of rewarding incorrect implementations with consistent behavior, doesn't seem a good trade off. I reported this discovery more as a curiosity than anything else. Feel free to close this issue if you want. |
This can be a very cheap check in |
@theodorzoulias would you be interested in contributing a fix? |
@eiriktsarpalis no, sorry. I don't have the environment ready, so I could only submit non-tested code, which I would prefer not to do. :-) |
Funnily enough I just found an answer in StackOverflow that uses a custom |
Description
Out of curiosity I experimented with the
List<T>.AddRange
method, by passing as argument a customICollection<T>
implementation with maximumCount
and no-opCopyTo
. The behavior changes depending on the existing size of the list, and may result in a negativeList<T>.Count
value.Reproduction Steps
Here is my custom
ICollection<T>
:Passing an instance to
List<T>.AddRange
I expected to get an exception of some kind, which is indeed what happens:Output:
But if the list is not empty before the
AddRange
, then there is no exception:Output:
Online demo.
Configuration
This is not a realistic scenario, since the
MyCollection<T>
is not correctly implemented, but nevertheless I thought I should report it.The text was updated successfully, but these errors were encountered: