3 ms·
2 main things: - Boxing/unboxing would not be an issue anymore, so you gain performance. - Runtime types are enforced: List myList = new List<String>(); myLis
by tehlike 9y ago
2 main things:
- 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.
- ygra 9y agoYou'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.
- tybit 9y agoI 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.
- eterm 9y agoTesting 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.
- tehlike 9y agoFor c#, try compiling with IList instead of list. IList myList = new List<String>(); myList.add(new Object());
- Sharlin 9y agoTo be fair, raw types like List are really deprecated and only exist in order to interface with legacy APIs.
- tehlike 9y agoJava? 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.
- TheCoelacanth 9y agoCan 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) { ... }