C# learnt from many mistakes that Java had.
- C# has decimal fixed point.
- C# has
(local variable type inference) since 3.0.var
- C# has real generics ("reified generics") since 2.0. Java has fake generics ("erasure generics") since Java 5.
- C# has real properties.
- C# has value types.
- C# has attributes since the very beginning. Java only has annotations (the Java equivalent to attributes) since Java 5.
- Ditto for for-each loops.
- Ditto for auto-boxing and auto-unboxing.
- Ditto for varargs.
- C# has
(and the dispose pattern, viausing
IDisposable
) since the very beginning. Java only has try-with-resources (the Java equivalent to
) since Java 7.using
- Ditto for string switches. Also note that C# switches are more versatile than Java's ones; you can use
andgoto case
to jump to other cases.goto default
- C# has lambdas since 3.0, long before Java did (Java 8).
- Ditto for extension methods. (The closest thing Java has to extension methods are interface default methods, and those are introduced in Java 8.)
- C# has static classes (non-instantiable classes where all methods have to be static) since 2.0. In Java, you can hack around it by making a class with only private constructors, but you can still define non-static methods in such a class.
- C# methods are non-virtual by default. In Java you can only make a method non-virtual by making it private or final.
- C# requires using
when overriding, and forbids usingoverride
when not overriding. This makes it easier to detect when a refactor is breaking other code.override
- C# has operator overloading.
- C# has compile-time constants using
. In Java, you can only have compile-time constants for numbers, booleans, characters, and strings.const
- C# does not have checked exceptions. Many programmers consider checked exceptions a broken concept.
- C# has named and optional arguments since 4.0.
- C# has
andref
parameters. In Java, everything is pass-by-value; it's not possible to have pass-by-reference.out
- C# has enumerator generators via
since 2.0.yield
Related : Learn C# from scratch
- Covariant and contravariant generic type parameters have been around since Java 5, but were introduced in C# 4.0.
- Java enumerations (which are full-fledged classes) are much more advanced than C# enumerations (which are basically just integers). For example, you can use it to implement a simple state machine.
- Java has
BigDecimal
which provides decimal floating point, as opposed to C#'s builtin decimal fixed point.
Yes. C# lacks some stuff java has but events with deleagetes, async/await or LINQ have advantage over type variance (covariance and contravariance) in java.
No comments :