Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most lightweight .NET collection

Tags:

c#

.net

c#-4.0

I wonder, what are the differences in collection implementations in .NET .

For instance, I constantly use List<int> etc to store a list of items. However I just need a container for items, and I guess I don't need all the features that List have. I just need an container that has put method, and will enable client code to iterate over the container.

Are there any quicker, more lightweight collection implementations that implement IEnumerable<T> in .NET?

like image 744
dragonfly Avatar asked May 30 '12 11:05

dragonfly


People also ask

Which collection is faster in C#?

ListDictionary is faster than Hashtable for small collections (10 items or fewer). The Dictionary<TKey,TValue> generic class provides faster lookup than the SortedDictionary<TKey,TValue> generic class.

What can I use instead of a list C#?

"In C#, an array is a list" That's not true; an array is not a List , it only implements the IList interface.

In which .NET type collection classes stores data in it?

A . NET collection is a set of similar type of objects that are grouped together. System. Collections namespace contains specialized classes for storing and accessing the data.


1 Answers

The most lightweight container which implements IEnumerable<T> is a typed array. It has no Add method (and therefore does not dynamically resize like a list does) but if you know what number of elements you want up front you can define the array and insert elements at a given position.

var myArray = new int[10];
myArray[0] = 123;
myArray[1] = 234;
like image 107
Jamiec Avatar answered Sep 30 '22 20:09

Jamiec