Skip to content

Adds Range Manipulation APIs for Collection<T> and ObservableCollection<T> #65101

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged,
[NonSerialized]
private int _blockReentrancyCount;

[NonSerialized]
private bool _skipRaisingEvents;

/// <summary>
/// Initializes a new instance of ObservableCollection that is empty and has default initial capacity.
/// </summary>
Expand Down Expand Up @@ -110,9 +113,115 @@ protected override void RemoveItem(int index)

base.RemoveItem(index);

OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
if (!_skipRaisingEvents)
{
OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
}
}

/// <summary>
/// Called by base class Collection&lt;T&gt; when a count of items is removed from the list;
/// raises a CollectionChanged event to any listeners.
/// </summary>
protected override void RemoveItemsRange(int index, int count)
{
CheckReentrancy();

T[] removedItems = Array.Empty<T>();

bool ignore = _skipRaisingEvents;
if (!ignore)
{
if (Items.IsReadOnly)
{
throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection);
}

if ((uint)index > (uint)Items.Count)
{
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_InvalidThreshold);
}

if (count < 0)
{
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_NeedNonNegNum);
}

if (index > Items.Count - count)
{
throw new ArgumentException(SR.Argument_ItemNotExist);
}

_skipRaisingEvents = true;

if (count > 0)
{
removedItems = new T[count];
for (int i = 0; i < count; i++)
{
removedItems[i] = this[index + i];
}
}
}

try
{
base.RemoveItemsRange(index, count);
}
finally
{
if (!ignore)
{
_skipRaisingEvents = false;
}
}

if (count > 0 && !_skipRaisingEvents)
{
OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItems, index);
}
}

/// <summary>
/// Called by base class Collection&lt;T&gt; when a collection of items is added to list;
/// raises a CollectionChanged event to any listeners.
/// </summary>
protected override void ReplaceItemsRange(int index, int count, IEnumerable<T> collection)
{
CheckReentrancy();

_skipRaisingEvents = true;

T[] itemsToReplace = new T[count - index];
for (int i = index; i < count; i++)
{
itemsToReplace[i] = this[i];
}

try
{
base.ReplaceItemsRange(index, count, collection);
}
finally
{
_skipRaisingEvents = false;
}

if (!_skipRaisingEvents)
{
IList newItems = collection is IList list ? list : new List<T>(collection);

if (newItems.Count > 0)
{
OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Replace, itemsToReplace, newItems, index);
}
}
}

/// <summary>
Expand All @@ -124,9 +233,51 @@ protected override void InsertItem(int index, T item)
CheckReentrancy();
base.InsertItem(index, item);

OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
if (!_skipRaisingEvents)
{
OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
}
}

/// <summary>
/// Called by base class Collection&lt;T&gt; when a collection of items is added to list;
/// raises a CollectionChanged event to any listeners.
/// </summary>
protected override void InsertItemsRange(int index, IEnumerable<T> collection)
{
CheckReentrancy();

bool ignore = _skipRaisingEvents;
if (!ignore)
{
_skipRaisingEvents = true;
}

try
{
base.InsertItemsRange(index, collection);
}
finally
{
if (!ignore)
{
_skipRaisingEvents = false;
}
}

if (!_skipRaisingEvents)
{
IList newItems = collection is IList list ? list : new List<T>(collection);

if (newItems.Count > 0)
{
OnCountPropertyChanged();
OnIndexerPropertyChanged();
OnCollectionChanged(NotifyCollectionChangedAction.Add, newItems, index);
}
}
}

/// <summary>
Expand Down Expand Up @@ -262,6 +413,14 @@ private void OnCollectionChanged(NotifyCollectionChangedAction action, object? i
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index, oldIndex));
}

/// <summary>
/// Helper to raise CollectionChanged event to any listeners
/// </summary>
private void OnCollectionChanged(NotifyCollectionChangedAction action, IList? items, int index)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, items, index));
}

/// <summary>
/// Helper to raise CollectionChanged event to any listeners
/// </summary>
Expand Down
Loading