Sunday, 18 September 2016

C# Vs Java

My experience with C# after working with Java for almost 5 years is :

1. Until Java 1.8, most of advanced features available in other languages such as C# were missing.

C# features that stand out because of coding experience -
1. Lambda/LINQ (Available in Java 1.8)
2. Extension Methods ( Its awesome feature where existing classes which can not be accessed because they may belong to .Net library/third party library, can be introduced with additional new methods, without touching its code. Surprised, why java did not have this feature. Coming from Java background, makes me little strange, even sealed classes are allowed to be extended in some way through Extension Methods)

Example :
static class Extension
{

     public static String substr(this String str, int startIndex, int lastIndex)
    {
          return str.substr(startIndex,lastIndex)
     }
}

3. Detecting DataType during compilation time using var in C#
4. optional parameter by setting default value to certain parameter in C#
5. Properties for getter/setter ; Example = public string Name {get;set;}. Here get/set can be controlled. If set is not defined, then Name becomes readonly. No need for methods like GetName(), SetName(String name);
6. out parameter
7. By default methods can not be overridden (Non Virtual). Explicit use of virtual/overridden to make method overridden. [I don't feel this is good feature. By default methods should be overridden, if a class is inherited. Java does not have concept of keywords virtual which will then allow method to be overridden, though annotation @override is possible for sake of clarity ]
8. nullable datatype ;
example - int ? x = null; provides property Value to access value. Also, HasValue will let one know, if x is assigned to some value or not assigned.


I really find coding experience in C# much better than Java. Having said that, I also like to make a point, languages that come later always know the shortcomings and come with its handling to overcome known limitation/shortcomings.
That is the case with C#, which is inspired by Java and took birth to compete with Java.
On other side, Java is opensource, very much popular, has large community, technology stack is hard to beat. Most of applications are built upon Java. Next thing is unbeatable - It's platform support/cross platform. Write once, run on many different Operating Systems.

No comments:

Post a Comment