Differences between C# and Java
Abstract:
This essay tries to avoid discussing the advantages and disadvantages of each language, but only discusses the differences. Please note , It is assumed that the reader is a java developer, and thus sometimes, only the C# feature has been mentioned as it would obvious for a experienced java developer, what it's counterpart in Java is. To cut to the chase, let's start focusing about the differences.
Language Differences:
- Java Has two types of inner classes, static inner classes and non-static inner classes. C# has a similar feature as static-inner classes but does not have any counterpart for non-static inner classes. Furthermore in java you could declare anonymous inner classes inside methods, but you can not do that in C#.
- Java allows fall-through in its switch statements and can switch on Integers and chars but C# can switch on Strings and longs and does not allow a fall-thorough unless it is empty.
- Java does not allow pointers and pointer arithmetic, but C# allows restricted use of pointer by using the unsafe keyword for that block of code that, programmer is going to use pointers.
- When it comes to primitive types, C# has more primitive types than java, supporting unsigned Integers and verbatim Strings.
- C# provides the ability to avoid the usage of escape sequences within string constants and instead declare strings literally. Thus backslashes, tabs, quotes and newlines can be part of a string without using escape sequences. The only caveat is that double quotes that appear within verbatim strings should be doubled. Verbatim strings are specified by prepending the @ symbol to string declarations.
- C# has true multidimensional, which java doesn't have, but they both have arrays of arrays (jagged arrays), have less access performance compared to multidimensional (rectangular) arrays.
- Partial class is a concept in C# that is missing in Java, which allows a class source to be split across several source files but must be marked partial.
- Generics exists in both programming languages but implemented different. In Java only the compiler knows about them and JVM did not have to change that much to support it but the mechanism is embedded in the .Net platform. Java does not permit primitive types in generics but C# does.
- Method pointers are included in C# in the form of delegates used for callbacks and event handling, which does not exist in Java.
- Event handling is done through Observer pattern in Java, but it is handled at language level in C# by using delegates.
- C# contains the goto statement to jump to certain labels, which does not exist in Java.
- C# allows operator overloading subject to restrictions, but java doesn't.
- In Java subclass methods with same name as their parent class method always override their corresponding methods in the super class, which is not true in C#. To override, one should use the override modifier, else it is considered as a new method.
- C# supports explicit interface method implementations, which help in the situation when a class implements several interfaces which have methods with same signatures.
- Preprocessor Directives give the ability to C# to compile certain sections of the code, under certain conditions. No equivalent in Java.
- C# does not support checked exception like java, and only has unchecked exception (runtime exceptions).
- Accessing com components exists in .Net, but in Java a third party bridge is needed. Working with native code is also easier in .Net as it is possible to disable safety restrictions on CLR.
- In Java all methods are virtual methods while in C#, one must explicitly state which methods one wants to be virtual since by default they are not. It is possible to mark methods as final in Java which means that the method cannot be overridden by derived classes. In C# this can be done by not marking the method as virtual.
- Javadoc is the tool used to extract API documentation from source code. Javadoc generates HTML documentation from the source code comment.C# uses XML as the format for the documentation. The generated documentation is an XML file that contains the metadata specified by the user with very little additional information generated automatically.
- Java inserts all class object onto the heap memory, while primitives on the stack. Putting objects on the heap have some inefficiency, which we do not have to do with C# as you could make a class stack based.
- Properties are a part of C# which is similar to getters and setters in Java which is not a mechanism but a pattern in Java. you could have more control over properties through read only, write only,…properties.
- An indexer is a special syntax for overloading the [] operator for a class in C# that does not exist in Java. An indexer is useful when a class is a container for another kind of object. Indexers are flexible in that they support any type, such as integers or strings, as indexes. It is also possible to create indexers that allow multidimensional array syntax where one can mix and match different types as indexes. Finally, indexers can be overloaded.
- The using keyword can be used to alias the fully qualified name for a type similar to the way typedef is used in C and C++. This is useful in creating readable code where the fully qualified name of a class is needed to resolve namespace conflicts, which does not exist in Java.
- In Java the arguments to a method are passed by value meaning that a method operates on copies of the items passed to it instead of on the actual items. In C#, as in C++ and in a sense C, it is possible to specify that the arguments to a method actually be references to the items being passed to the method instead of copies.
- C# provides the option to explicitly detect or ignore overflow conditions in expressions and type conversions. Overflow conditions detected in code throw a System.OverFlowException.
- A static class is a class that has no instance members, no instance constructors and cannot be used as a base class. There are no static classes in Java.
- In C# a nullable types is an instance of the System.Nullable type. A nullable type adds the possibility of a variable being null in addition to its original value range. Nullable types are particularly useful when mapping C# objects to relational database schemas since null is a valid value for all data types in SQL databases. There is no such concept in Java.
- Anonymous methods are a companion feature to delegates. An anonymous method is a code block that can be used where a delegate method is expected. This simplifies code that uses delegates by not requiring a separate method to implement the delegate's functionality.
- Creating a data structure to be compatible to be used in a foreach loop , is somehow cumbersome in both Java and C# programming languages, but C# could make this less painful converting a method or property into an iterator through keyword yield.
- At the time of this writing C# is only available on Windows. Efforts are currently in place to port it to other platforms, including Linux and FreeBSD.
- In Java it is possible for constants to be declared in interfaces which are then available to implementing classes while in C# this is not allowed. This may not be a big issue in C# since the primary usage of constants declared in interfaces is as a poor emulation of enumerations.