How are elements stored in containers in .Net? - c#

How are elements stored in containers in .Net?
For example, a C++ vector stores in sequential order, while List doesn't.
How are they implemented for .Net containers (Array, ArrayList, ...)?
Thanks.

It depends on the element. But a C++ Vector is equivalent to a C# List, and a C++ List<T> is equivalent to a C# LinkedList
The C# ArrayList is pretty much, a C# List<object>
Wikipedia lists many data structures, and I suggest you have a look there, to see how the different ones are implemented.
So:
C++ C# How
Vector ArrayList / List Array (sequential)
List LinkedList Linked List (non-sequential, i.e. linked)

It varies by container. Because the implementation is proprietary, there are no public specs mandating what the underlying data structure must be.
If you're interested in taking the time, I've used the following tools before to understand how MS implemented this class or that:
Debugging with the Microsoft .NET Framework debugging symbols.
Inspecting assemblies with Reflector.

In .net, containers (even arrays) handle all access to them. You don't use pointers to work with them (except in extremely rare cases you probably won't ever get into), so it often doesn't matter how they store info. In many cases, it's not even specified how things work behind the scenes, so the implementation can be changed to something "better" without breaking stuff that relies on those details for some stupid reason.
Last i heard, though, arrays store their entries sequentially -- with the caveat that for reference-type objects (everything that's not a struct), "entries" are the references as opposed to the objects themselves. The data could be anywhere in memory. Think of it more like an array of references than an array of objects.
ArrayLists, being based on arrays, should store their stuff the same way.

Related

ImmutableSortedDictionary range enumeration by key

I was reading about C#'s ImmutableSortedDictionary in System.Collections.Immutable and thinking about how to apply it in my program. I quite like C++'s lower_bound and upper_bound (see here), and I was rather expecting to see something of the sort for range lookups. However, similar methods seem to be strangely absent from the documentation. Am I missing something? Or does MS truly provide a sorted dictionary without efficient access to the sorted ranges? That doesn't exactly seem like something one could do on an IEnumerable of the keys as say an extension method, so I'm a bit puzzled I'm not seeing something provided directly by the collection.
It is irritating that the available built-in collections are not offering a full set of features (like the SortedDictionary lacking a BinarySearch method), forcing us to search for third-party solutions (like the C5 library).
In your case instead of an ImmutableSortedDictionary you could probably use a ImmutableSortedSet, embedding the values in the keys and using an appropriate comparer. At least the API of this class contains the properties Min and Max.

Do C# dictionaries use hashing in the same manner as Java HashMaps?

I understand that Java HashMap elements are stored in "buckets" based on the hashes of the elements' keys. Does that same hashing occur in C# dictionaries? If not, then how does lookup work?
There are several kind of Dictionarys inside C# System.Collections - namespace. They use different strategies to store theire internal data:
This one System.Collections.Specialized.HybridDictionary
uses DoubleLinkedLists until a certain size is reached, then switches to Hashsets. The "normal" System.Collections.Generics-Dictionary uses Hashsets internally all the time. There is also a Dictionary for concurrent uses - look it up yourself (Concurrent Dictionary)if you like.
So it depends on what kind of Dictionary you are using and (in above case) might change due to internal considerations of the class you are using for performance or other reasons.

Best structure for a growing collection of objects in C#?

For my Game Programming class, I am designing a class for a player character. The player can collect power-ups throughout the game, which are held onto and can be used whenever the player chooses.
I thought a dynamic array would work well, but my background is in C++ (we are using Unity in class, which uses Java & C#), and I know that memory deallocation is handled differently in C# (I know there is a garbage collector, but don't really know much about how it functions). After looking around the web a while, I couldn't find anything that seemed to fit the functionality I need (or if I did it was over my head and I didn't realize it).
If someone can list a C# structure, or structures, that would be good for storing a growing collection of objects, it would be extremely helpful.
List is probably the simplest structure you could use, it is like a dynamic array which will automatically grow as you add things to it. It is strongly typed so it will only contain objects of the same type (if you have some interface for your power-ups you can have a list of IPowerUp instances)
Start by looking at .NET's generic collections. (.NET generics are similar in concept to C++ templates.) Either List or Dictionary will likely be useful to you depending on how you need to store and retrieve your objects.
Since you'll probably have types of objects you may consider using a Dictionary of Lists where the key will be a string identifier or an enumeration of types of collected objects and the value will be a list of object instances.

System.Linq.Lookup vs. Wintellect.PowerCollections.MultiDictionary

I still use Wintellect's PowerCollections library, even though it is aging and not maintained because it did a good job covering holes left in the standard MS Collections libraries. But LINQ and C# 4.0 are poised to replace PowerCollections...
I was very happy to discover System.Linq.Lookup because it should replace Wintellect.PowerCollections.MultiDictionary in my toolkit. But Lookup seems to be immutable! Is that true, can you only created a populated Lookup by calling ToLookup?
Yes, you can only create a Lookup by calling ToLookup. The immutable nature of it means that it's easy to share across threads etc, of course.
If you want a mutable version, you could always use the Edulinq implementation as a starting point. It's internally mutable, but externally immutable - and I wouldn't be surprised if the Microsoft implementation worked in a similar way.
Personally I'm rarely in a situation where I want to mutate the lookup - I would prefer to perform appropriate transformations on the input first. I would encourage you to think in this way too - I find myself wishing for better immutability support from other collections (e.g. Dictionary) more often than I wish that Lookup were mutable :)
That is correct. Lookup is immutable, you can create an instance by using the Linq ToLookup() extension method. Technically even that fact is an implementation detail since the method returns an ILookup interface which in the future might be implemented by some other concrete class.

Fast exchange of data between unmanaged code and managed code

Without using p/invoke, from a C++/CLI I have succeeded in integrating various methods of a DLL library from a third party built in C.
One of these methods retrieves information from a database and stores it in different structures. The C++/CLI program I wrote reads those structures and stores them in a List<>, which is then returned to the corresponding reading and use of an application programmed completely in C#. I understand that the double handling of data (first, filling in several structures and then, filling all of these structures into a list<>) may generate an unnecessary overload, at which point I wish C++/CLI had the keyword "yield".
Depending on the above scenario, do you have recommendations to avoid or reduce this overload?
Thanks.
You do not need the yield keyword to create iterators. Just create one class implementing IEnumerator<T> and another class implementing IEnumerable<T>.

Categories

Resources