When you're doing a direct cast like that, if the variable is not of type List<Post> or a descendant, then the cast will not succeed. Your "posts" variable is apparently of type Post[] or Collection<Post> or ArrayList or something other than List<Post>, which means a direct cast is impossible. Collection types aren't generally interchangeable.
What you can do is: List<Post> myPosts = new List<Post>(posts); This takes the collection of Post objects inside of posts and adds them to a new list. It's not a cast, but rather a conversion. On Mar 20, 3:47 am, graphicsxp <[email protected]> wrote: > Hello, > > I've got two classes, CGMPost and MSMPost, which both inherits from a > class Post. > > I have a IList called 'posts' that contains a collection of CGMPost > and MSMPost entities. > > If I do that : > > List<Post> myPosts = posts as List<Post>; > > then myPosts is null (altough posts contains 7 entities of type > CGMPost and MSMPost). > > Why is that ?
