Hi, I got a job in c# without any previous knowledge of the language but I'm doing just fine with most it – looks a lot like Java and C++. There are a couple of things I still don't get, though.
In Java I cannot do this:
List<Object> myList = new LinkedList<String>(); //compiler error
But I can if I declare it in a slightly different way:
List<? extends Object> myList = new LinkedList<String>(); //ok
In C# I get the same error if I try something like this:
LinkedList<object> myList = new LinkedList<string>(); //compiler error
But the "?" syntax does not appear to exist in C#. Is there an alternative way to do it?
I've heard about Cast<T>() and googled about it, but if possible I'd appreciate a solution that is not specific to the case of a list (i.e. something that works with custom classes defined in the code I'm working on).
by redrainyskies via /r/csharp