Skip to content

Instantly share code, notes, and snippets.

@svick
Last active August 29, 2015 14:20
Show Gist options
  • Save svick/8c2696222800aa031450 to your computer and use it in GitHub Desktop.
Save svick/8c2696222800aa031450 to your computer and use it in GitHub Desktop.
It's not safe to call ToList on IsReadOnly collection
var cdict = new ConcurrentDictionary<int, int>();
var t = Task.Run(() => {
for (int i = 0; i < 100000; i++)
cdict[i] = i;
});
IEnumerable<KeyValuePair<int, int>> kvps = new ReadOnlyDictionary<int, int>(cdict);
((ICollection<KeyValuePair<int, int>>)kvps).IsReadOnly.Dump(); // true
while (!t.IsCompleted)
kvps.ToList(); // almost always throws ArgumentException in CopyTo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment