Method / Casting Problem
static void sort(ilist<icomparable> list)
{
// method implementation
}
that works expect except must ugly cast every time call method:
list<string> somestrings = new list<string>();
// add items
sort((ilist<icomparable>)somestrings);
i want way this:
sort(somestrings);
my problem more casting else. there way can tell compiler want objects matching ilist<icomparable> type automatically cast?
hope clear asking. appreciated.
> works expect except must ugly cast every time call method:
that code not work; @ run time it fails follows:
unable cast object of type 'system.collections.generic.list`1[system.string]' type 'system.collections.generic.ilist`1[system.icomparable]'.
you see, ilist<icomparable> not interface implemented list<string> cast fails. (this common misunderstanding of how generics work.)
however, following signature should want:
static void sort<t>(ilist<t> list) where t : icomparable
{
// method implementation
}
.NET Framework > Common Language Runtime Internals and Architecture
Comments
Post a Comment