Method / Casting Problem


i wish write sorting algorithm work list of comparable items.  figure method definition should this:

code snippet

static void sort(ilist<icomparable> list)
{
  // method implementation
}



that works expect except must ugly cast every time call method:

code snippet

list<string> somestrings = new list<string>();
// add items
sort((ilist<icomparable>)somestrings);



i want way this:

code snippet

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:

 

code snippet

static void sort<t>(ilist<t> list) where t : icomparable

{

// method implementation

}

 

 

 



.NET Framework  >  Common Language Runtime Internals and Architecture



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to send non-standard Content-Type header ?