Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how many times have you created a dictionary that looks like this:

C#
MyClass a;
var data = new Dictionary<string, MyClass>();
data.Add(a.Name,a);


Or maybe worse:

C#
var data = new Dictionary<string, string>();
data.Add(a,a);


what if we could create a Dictionary that bound the key to a member?
something like:

C#
MyClass a;
var data = new MemberBindingDictionary<MyClass>("Name");
data.Add(a);
if(data.ContainsKey(a.Name))
{
...
}


I couldn't find anything like it. Is that because it's a stupid idea? or is it too expensive?
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-13 16:06pm    
Not clear what's the problem. It happens all the time and is very usual. How can it possibly compromise performance?
This question is not needed at all, you should just speculate by definition, and you will understand what happens.
—SA

Hi,

There's not a class like that in C#. But you can create a class like that yourself:
C#
public class MemberBindingDictionary<T>:Dictionary<object, T>
    {
        Func<T,object> _binding = null;
        public MemberBindingDictionary(Func<T,object> binding)
        {
            _binding = binding;
        }
        public void Add(T value)
        {
            this.Add(_binding(value), value);
        }
    }

I don't use a string as a parameter in the constructor, but a Func. Now, you can use this class, and bind the value to a key using a lambda expression:
C#
MyClass a;
var data = new MemberBindingDictionary<MyClass>(x => x.Name);
data.Add(a);
if(data.ContainsKey(a.Name))
{
...
}

So, don't write "Name", but write x => x.Name. This is a lambda expression[^]

Hope this helps.

[Edit]Formatting problems fixed[/Edit]
 
Share this answer
 
v2
Comments
Maciej Los 4-Mar-13 13:29pm    
BIG 5!
Thomas Daniels 4-Mar-13 13:34pm    
Thank you!
Yossi Yaari 4-Mar-13 13:40pm    
that's definitely the direction I was thinking.
just was wondering how bad the implementation would turn out. mostly in terms of performance.

and of course you could add a ContainsByKey using the same binding func. :-)
Thomas Daniels 4-Mar-13 13:42pm    
You don't must declare a ContainsKey method yourself. Because I inherit from Dictionary, this method is inherited already.
Yossi Yaari 5-Mar-13 0:48am    
what I mean is that you could add a method that accepts object of Type <t> and calculates the key by itself using the binding method. so that in theory, if the binding changed, you could leave the code the same.

MyClass a;
var data = new MemberBindingDictionary<myclass>(x => x.Name);
data.Add(a);
if(data.ContainsKey(a)) //<-- would stay true for any change of ^^^ binding method
{
...
}
I would probably just use link with an array of MyClass like this.

C#
IEnumerable<myclass> data = new MyClass[] { new MyClass("Greg"), new MyClass("Peter"), new MyClass("Bobby") };

if (data.Count(x => x.Name == "Peter") > 0)
{
   // Do something.
}
 
Share this answer
 
v3
Comments
Yossi Yaari 5-Mar-13 0:49am    
that would have very slow performance.
O(n) instead of O(1)
BC @ CV 5-Mar-13 17:24pm    
"Premature optimization is the root of all evil" -- DonaldKnuth
In my 10 years as a programmer I have rarely had a serious problem with code that performed too slow and have regularly had headache inducing problems with overly complicated and hard to maintain code. Unless the project requires super optimized performance I always go for the simplest most easy to read solution.
Yossi Yaari 10-Mar-13 11:42am    
I do agree with you in principal.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900