Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How do I add type filters to a .NET Dictionary so that the TValue will be a class object that inherits my EventListener interface? So for example

Dictionary<string, object> events = new Dictionary<string, object>(); //where object would have to inherit EventListener
Posted
Updated 13-Aug-13 10:47am
v2

1 solution

Look at following sample:
C#
interface I {};

class a : I {};
class b : I {};
class c {};

void Main()
{
    Dictionary<string, I> x = new Dictionary<string, I>();
    x.Add("a", new a());
    x.Add("b", new b());
    x.Add("c", new c()); //will fail
}
 
Share this answer
 
Comments
Sicppy 13-Aug-13 17:06pm    
Thank you much! Let me test, but if it works ill accept the solution!
Zoltán Zörgő 13-Aug-13 17:07pm    
It works, I made this demo in LinqPad.
Sicppy 13-Aug-13 17:15pm    
Yeah, i was just making sure. Thanks again!

-Jordan
Sergey Alexandrovich Kryukov 13-Aug-13 17:25pm    
Sure, a 5.
—SA

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