(Java programmer trying to understand generics in C#) How do you assign a list of strings to a pointer declared as list of objects?

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

1 Comment

  1. Sahiba says: Reply

    In Ubuntu 7.10 Gutsy, executing a Mono apiiacltpon can result in: bash: ./Foo.exe: cannot execute binary fileOr if using sh: ./Foo.exe: 1: MZBut if you install the binfmt-support package, it will work.openSUSE and SLED probably have a similar package.

Leave a Reply