Created
April 19, 2011 11:18
-
-
Save shiftkey/927154 to your computer and use it in GitHub Desktop.
Comparing the behaviour between IEnumerable<T> and List<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ObservableCollection(IEnumerable<T> collection) | |
{ | |
this._monitor = new SimpleMonitor<T>(); | |
if (collection == null) | |
{ | |
throw new ArgumentNullException("collection"); | |
} | |
this.CopyFrom(collection); | |
} | |
public ObservableCollection(List<T> list) | |
: base((list != null) ? new List<T>(list.Count) : list) | |
{ | |
this._monitor = new SimpleMonitor<T>(); | |
this.CopyFrom(list); | |
} | |
// which both use | |
private void CopyFrom(IEnumerable<T> collection) | |
{ | |
IList<T> items = base.Items; | |
if ((collection != null) && (items != null)) | |
{ | |
using (IEnumerator<T> enumerator = collection.GetEnumerator()) | |
{ | |
while (enumerator.MoveNext()) | |
{ | |
items.Add(enumerator.Current); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment