locked
LINQ to Objects Cast<> problem

Answers

  • The Cast<T> operator only performs reference and boxing conversions. It specifically does not consider user defined conversions. Use the Select operator to perform those:

     

        myFoos.Select(foo => (Bar)foo);

     

    Anders

     

    Sunday, January 27, 2008 11:26 PM

All replies

  • The Cast<T> operator only performs reference and boxing conversions. It specifically does not consider user defined conversions. Use the Select operator to perform those:

     

        myFoos.Select(foo => (Bar)foo);

     

    Anders

     

    Sunday, January 27, 2008 11:26 PM
  • specifically? are you sure?

    it's called Cast, surely it should consider custom cast operations. It's not that difficult.

    I'm not a regular to these forums. Do members of the LINQ team visit these forums?

    Could you concider this for the next release?

    Thanks for the work-around tho.
    Sunday, January 27, 2008 11:45 PM
  • I've just ran into this same problem. I understand it can't be done, but why can't it be done? Is it for performance?

     

    Tuesday, January 20, 2009 10:01 AM
  • Here's a discussion from this forum about this change.

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3895492&SiteID=1

     

    Note that Cast's behavior changed from 3.5 to 3.5SP1, so you could have code that breaks if you test in one but not the other.

     

    Wednesday, January 21, 2009 5:50 AM