It really helps to quote the message you're replying to! Less work for
everybody else. ;-)
This does not
Type does support a "contains" method like the IList. Is there a
reason why?
Of course there is. However, I don't know what that reason is, offhand. Do
you really need to know why? If you spend enough hours studying the .Net
Framework SDK, and Googling, eventually you may figure it out. But it won't
bring your project any closer to completion. On the other hand, you will
have learned a great deal about the .Net platform and the CLR, so it won't
have been entirely wasted! ;-)
If you want to implement a "Contains" method for it, just write one.
Example:
class MyDictionary : System.Collections.Generic.Distionary<int, MyClass>
{
public bool Contains(MyClass instance)
{
foreach (MyClass c in Values)
if (c.Equals(instance)) return true;
return false;
}
}
Note that you would have to implement your own Equals override for MyClass
if you don't want to specify that the 2 references point to the same class
instance.
If wanted to return an IList, how can i do that?
IList is an interface. It is implemented in quite a few types. What would
you want to return one from? A method or a property?
This is your original question:
If that can't be done, how can i instantiate and add values to a
collection declared as IDictionary< int, MyCalss>.
Also, is there a way to declare a type or alias for "IDictionary<int,
MyClass>", so i don't have to type that declaration everytime i need to
declare a variable of that particular collection?
I presumed you wanted to implement a strongly-typed Dictionary class, and
that you wanted to avoid having to pass a type parameter to it whenever you
used it. My answer was:
class MyDictionary : System.Collections.Generic.Distionary<int, MyClass>
{
}
This class inherits a strongly-typed Generic Dictionary class with an
integer type Key, and a MyClass type Value. If you wanted an IList, why
didn't you say so? Or, are you just asking out of curiosity? In any case,
the answer is, if you want to write a method that returns an IList of some
kind or other, write one.
Would i need to write
my own generic class for this?
Are you sure you know what a Generic class is? An IList is not a generic
type. Why would you need to write a Generic class to create a method that
returns a specific type?
Maybe I'm just not understanding what it is you need. Perhaps if you can be
more specific, I can help.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.