7

Is there any way to initialize the capacity of an ObservableCollection<T> like you can a List<T>?

For example:

var observableCollection = new ObservableCollection<int>(100);
declouet
  • 307
  • 3
  • 10
  • Did you look at the documentation? http://msdn.microsoft.com/en-us/library/ms658737(v=vs.110).aspx – Brian Rasmussen Apr 14 '14 at 20:03
  • 1
    Why do you want to do this? – tnw Apr 14 '14 at 20:04
  • 1
    I suspect that `new List(100)` *doesn't* do what is expected - it creates a list with an initial [*Capacity*](http://msdn.microsoft.com/en-us/library/y52x03h2(v=vs.110).aspx) (which is rarely needed) and *not* "100 default elements" (which would affect the [*length/Count*](http://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx)). – user2864740 Apr 14 '14 at 20:09
  • 3
    I want to do this because of what I read in the List documenatation on MSDN "If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List." – declouet Apr 14 '14 at 20:30
  • @tnw: It is more efficient if you have a list that is large to not have to continually grow the array that the list uses underneath. You should look into List.Add to see what goes on underneath. This can greatly reduce memory allocations and peformance when loading large sets of data. – Mike Apr 14 '14 at 20:39

2 Answers2

10

No there is not. One of the constructors for ObservableCollection<T> takes a List<T>, so you might think you can do this:

new ObservableCollection<int>(new List<int>(100));

But that doesn't work. Internally, ObservableCollection copies the list and doesn't take into account any capacity properties on the list. You can verify this by looking at the constructors using .NET Reflector.

Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • I looked into the constructor like you suggested and saw that it does initialize the internal list to the same length as the list you pass in. Thanks. – declouet Apr 14 '14 at 20:50
  • 4
    @user2449323 Yes, but **the length of the list being passed in here is zero, not 100**. The *capacity* of the list is 100, but the length is zero as no items have been added to the list. – Servy Apr 14 '14 at 21:00
1

This is an old question. But, I thought it might be worthwhile to know that currently, it is possible to set the backing List capacity at the risk of relying on the internal implementation.

The Items property that is exposed by the base Collection<T> class is currently implemented as a List<T>. With type casting, you can set that property's Capacity.

Note that this does rely on the current implementation of both the List<T> and ObservableCollection<T> classes. If it is critical that you be able to set the backing Capacity and not worry that the .NET implementation will change (for instance, changing the backing list to a Dictionary or HashSet), you should instead create a new class that implements your needed interfaces.

You can use the Microsoft Reference Source as well as .NET Reflector.

Martin Soles
  • 534
  • 3
  • 8