7 ms·
One case where C# is far superior to Java is generics. Generics were a cutting-edge feature in 2005. Microsoft took the opportunity to start from scratch, and
by chadzawistowski 11y ago
One case where C# is far superior to Java is generics.
Generics were a cutting-edge feature in 2005. Microsoft took the opportunity to start from scratch, and released the .Net Framework 2.0 without backwards-compatibility, and with full support for generics. A List<String> is actually a List of String.
When Sun released Java 5, they didn't want to disrupt the established Java ecosystem. Their implementation is called "type erasure". As an example, List<String> is actually just List<Object>, with automatic runtime typecasting to String. https://docs.oracle.com/javase/tutorial/java/generics/erasure.html https://docs.oracle.com/javase/tutorial/java/generics/erasur...
Java's solution works well enough, but it breaks down in certain scenarios, such as multiple levels of generics. For example, when getting an element from a List<List<String>>, Object is not cast to List<String> (which doesn't really exist), but merely List.
C#'s implementation of generics became even more sophisticated with C#4, which introduced covariance and contravariance. https://msdn.microsoft.com/en-us/library/ee207183.aspx https://msdn.microsoft.com/en-us/library/ee207183.aspx
- santaclaus 11y ago> Generics were a cutting-edge feature in 2005. Generics have been around since at least the early '80s.
- chadzawistowski 11y agoWell, "cutting-edge" in enterprise technology. Another interesting failed prototype on the JVM was the language "Pizza" https://en.wikipedia.org/wiki/Pizza_%28programming_language%29 https://en.wikipedia.org/wiki/Pizza_%28programming_language%...
- nostrademons 11y agoAda isn't enterprise technology? https://en.wikibooks.org/wiki/Ada_Programming/Generics https://en.wikibooks.org/wiki/Ada_Programming/Generics http://www.cs.dartmouth.edu/reports/TR86-104.pdf http://www.cs.dartmouth.edu/reports/TR86-104.pdf (From 1984.)
- lern_too_spel 11y agoWhen does it matter what an element from a List<List<String>> is cast to at runtime? In a properly-written statically-typed program, types are a compile-time construct (ignoring the machinery of dynamic linking and method call semantics in particular implementations, which don't matter to the programmer), and though you may use them at runtime via reflection, I would argue that 99% of the time that you do, you're writing bad code. http://stackoverflow.com/questions/20918650/what-are-the-benefits-of-javas-types-erasure http://stackoverflow.com/questions/20918650/what-are-the-ben...
- dkharrat 11y agoType erasure introduces limitations on what you can do at runtime that forces a developer to write code to workaround it. For example, in Java you can't write a generic method that instantiates an object based on the type of the generic. The workaround is typically to pass in the Class type as an additional parameter to the method, which in theory should not be necessary. A lot of JVM-based languages are required to introduce additional complexity in the language to simulate reified generics (e.g. Scala's Manifest/TypeTag classes).
- airless_bar 11y ago> A lot of JVM-based languages are required to introduce additional complexity in the language to simulate reified generics (e.g. Scala's Manifest/TypeTag classes). I'm not sure how this makes sense. These tags are just standard context bounds, which are extremely useful and quite awesome. You make it sound as if these tags were some kind of special feature added to the language, or context bounds were solely invented to support these tags.
- cakoose 11y ago> For example, in Java you can't write a generic method that instantiates an object based on the type of the generic. The workaround is typically to pass in the Class type as an additional parameter to the method, which in theory should not be necessary. If a method needs to instantiate an object, the normal technique is to accept a factory function as a parameter. You can do the same thing with type-erased generics. C# decided to create a shortcut for calling the constructor of a generic type parameter. But, for example, you can't call static methods of a generic type parameter (and a constructor is really just a slightly special static method).
- pjmlp 11y ago> Generics were a cutting-edge feature in 2005. I was already using C++ templates in 1994 with Borland C++ for Windows 3.1! Ada, CLU and ML go back to the late 70's / early 80's.
- raverbashing 11y agoYes, but I guess that code would break in a million ways today in a modern C++ compiler It certainly worked at the time, for that specific compiler
- pjmlp 11y agoIt was still a language feature even if it wasn't available 100% bug free in all C++ compilers. C code also used to break left and right long after C90 was approved. I had to use K&R C style code in 1999 because the HP-UX compiler at a customer location didn't knew any better, but that didn't mean other C features weren't available elsewhere.
- raverbashing 11y ago> I had to use K&R C style code in 1999 because the HP-UX compiler at a customer location didn't knew any better, but that didn't mean other C features weren't available elsewhere. Curious, what did you need to do specifically? Parameter declarations weren't inlined? But anyway, C, being smaller, breaks in fewer ways
- pjmlp 11y ago> Curious, what did you need to do specifically? Parameter declarations weren't inlined? In those days HP-UX C compiler was still transitioning between K&R and ANSI standards. We could only properly compile without strange errors while using K&R function declarations. If memory serves me right, it was the first HP-UX release having support for 64bit file systems.
- pkolaczk 11y agoTemplates are not proper generics. Templates can be used to emulate generics, but they are not the same thing at all. Templates are not first-class citizen of the type system (they don't survive to the typechecking phase), while generics are. You cannot write a generic list implementation in C++ and have it typechecked.
- pkolaczk 11y agoOne case where Java generics are far superior to C# (actually CLR) is implementing more advanced generics on top of JVM thanks to erasure. C# generics are too weak to express concepts found in Scala or Haskell type system directly, but at the same time too strong to serve as a base for a standalone implementation of such type systems.