How C# is Different From Java

Features of C# absent in Java

  • C# includes more primitive types and the functionality to catch arithmetic exceptions.

  • Includes a large number of notational conveniences over Java, many of which, such as operator overloading and user-defined casts, are already familiar to the large community of C++ programmers.

  • Event handling is a "first class citizen"; it is part of the language itself.

  • Allows the definition of "structs", that are similar to classes but may be allocated on the stack (unlike instances of classes in C# and Java).

  • C# implements properties as part of the language syntax.

  • C# allows switch statements to operate on strings.

  • C# allows Anonymous methods providing closure functionality.

  • C# allows iterators that employ co-routines via a functional-style yield keyword.

  • C# has support for output parameters, aiding in the return of multiple values, a featured shared by C++ and SQL.

  • C# has the ability to alias namespaces.

  • C# has "Explicit Member Implementation" that allows a class to specifically implement methods of an interface, separate to its own class methods. This allows it also to implement two different interfaces that happen to have a method of the same name. The methods of an interface do not need to be "public"; they can be made to be accessible only via that interface.

  • C# provides integration with COM.

  • Following the example of C and C++, C# allows call by reference for primitive and reference types.

Features of Java absent in C#

  • Java's strictfp keyword guarantees the result of floating point operations remain the same across platforms.

  • Java supports checked exceptions for better enforcement of error trapping and handling.

Philosophical differences between the languages

  • There are no unsigned primitive numeric types in Java. While it is universally agreed that mixing signed and unsigned variables in code is bad, Java's lack of support for unsigned numeric types makes it somewhat unsuited for low-level programming.

  • C# does not include checked exceptions. Some would argue that checked exceptions are very helpful for good programming practices. Others, including Anders Hejlsberg, chief C# language architect, argue that they were to some extent an experiment in Java and that they haven't been shown to be worthwhile [1] [2].

  • C# namespaces are more similar to those in C++. Unlike Java, the namespace does not specify the location of the source file. (Actually, it's not strictly necessary for a Java source file location to mirror its package directory structure).

  • C# includes delegates, whereas Java does not. Some argue that delegates complicate the method invocation model, because they are handled through reflection, which is generally slow. On the other hand, they can simplify the code by removing the need to declare new (possibly anonymous) classes to hook to events.

  • Java requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file.

  • C# allows the use of pointers, that some language designers consider to be unsafe, but certain language features try to ensure this functionality is not misused accidentally. Pointers also greatly complicate technologies such as Java's RMI (Remote Method Invocation), where program objects resident on one computer can be referenced within a program running on an entirely separate computer. Some have speculated that the lack of memory pointers in Java (substituted by the more abstract notion of object references) was a nod towards the coming of grid computing, where a single application may be distributed across many physical pieces of hardware.

  • C# supports the goto keyword. This can occasionally be useful, but the use of a more structured method of control flow is usually recommended.

  • C# has true multi-dimensional arrays, as well as the array-of-arrays that is available to Java (that C# calls jagged arrays). Multi-dimensional arrays are always rectangular (in the 2D case, or analogous for more dimensions), whereas an array-of-arrays may store rows (again in the 2D case) of varyiing lengths. Rectangular arrays may speed access if memory is a bottleneck (there is only one memory reference instead of two; this benefit is very dependent on cache behavior) while jagged arrays save memory if it's not full but costs (at the penalty of one pointer per row) if it is. Rectangular arrays also obviate the need to allocate memory for each row explicitly.

  • Java does not include operator overloading, because abuse of operator overloading can lead to code that is harder to understand and debug. C# allows operator overloading, that when used carefully, can make code terser and more readable. Java's lack of overloading makes it somewhat unsuited for certain mathematical programs. Conversely, .NET's numeric types do not share a common interface or superclass with add, subtract and so on methods, restricting the flexibility of numerical libraries.

  • Methods in C# are non-virtual by default. In Java however, methods are virtual by default. Virtual methods guarantee that the most overridden method of an object will be called that is determined by the runtime. You always need to keep that in mind when calling or writing any virtual method! If the method is declared as non-virtual then the method to invoke will be determined by the compiler. This is a major difference of philosophy between the designers of the Java and .NET platforms.

  • Generics of Java 1.5 use type-erasure. Information about the generic types is lost when Java source is compiled to bytecode. .NET 2.0's generics are preserved after compilation due to generics support starting in version 2.0 of the .NET Common Language Runtime (CLR). Java's approach allows Java 1.5 binaries to be run in the 1.4 JRE, at the cost of additional runtime typechecks.

  • C# is defined by ECMA and ISO standards, whereas Java is proprietary, though largely controlled through an open community process.

  • The C# API is completely controlled by Microsoft, whereas the Java API is managed through an open community process.

  • The .NET run-time allows both managed and unmanaged code, enabling certain classes of bugs that do not exist in Java's pure managed code environment but also allows interfacing with existing code.

Up Next
    Ebook Download
    View all
    Learn
    View all