Bidirectional map in .NET.

Is there a .NET data structure that I could use for bidirectional search?

Here's the problem: Serialization. My object contains a field that points to one of 10 predefined static objects. When writing to a file, I write a single character representing which of the 10 objects is referenced. At this point, I need a search data structure that will allow me to get the character code based on the object reference. When deserializing, I need to do the opposite. I can think of many other places where I could use such a data structure.

+6
collections data-structures containers
source share
2 answers

In the case of only 10 cases that will rarely change, several methods using Switch statements may suffice.

If you have control over static objects, they can all implement a new interface that returns the character "serialization code":

public interface IStaticObject { char SerializationCode { get; }; } 

Therefore, moving in this direction is easy: someObject.SerializationCode. Then you can also use your static objects using a constructor that registers its SerializationCode with a singleton instance that has a dictionary.

 public class SomeStaticObject : IStaticObject { public void SomeStaticObject() { StaticObjectRegistrar.Register(this.SerializationCode, this); } public char SerializationCode { get { return ?; } } } 

Deserializing, you just take a character and run it through this dictionary to return a static object.

+5
source share

I would create a data structure that contains two common Dictionary objects that reflect each other so that the key of one represents the value of the other and vice versa. This would make it possible to search for O(1) in both directions.

+6
source share

All Articles