Testing this, it doesn't throw in C#, it just doesn't compile because you can't declare a variable to be a List without declaring the type with it. If you specify myList as List<String> then it still doesn't compile.
For c#, try compiling with IList instead of list.
IList myList = new List<String>(); myList.add(new Object());
To be fair, raw types like List are really deprecated and only exist in order to interface with legacy APIs.
Java? In a lot of cases you still need them. Anything that touches reflections is in that category, plenty of di/ioc systems, or mappers, etc relies on them.
Re:How generics were added to .NET
by TheCoelacanth on Tuesday March 06, 2018 @12:17PM
(#16530208)
Can you give an example?
Most of the time when reflection is needed I see something like this instead of just a plain list type.
<T> void foo(List<T> list, Class<? extends T> type) { ... }You'd have to call the Java variant via reflection to show the difference at runtime, as the compiler would already enforce that myList.add(new Object()) won't compile. The point stands, though.
Reflection is not required. I actually had the code, somehow got deleted
List myList = new ArrayList<String>(); myList.add(new Object());
this would compile.
Ah, I missed the missing generics on the declaration. Sorry.
I would say java and c# have both failed on your second point, separate to their respective VMs issues.
It would be nice if mistakes like that were deprecated then eventually fixed, even if over many years/versions.
- Boxing/unboxing would not be an issue anymore, so you gain performance.
- Runtime types are enforced: List myList = new List<String>(); myList.add(new Object());
In JAVA, this is ok. In c#, it throws.