C#, C++ and Java are very similar in nature. For example the basic syntax of all of the three languages is almost identical.
The focus of this lesson will be on the major similarities of Java and C#.
- C# and Jave both compile into a machine independent code, which then runs in a managed execution environment. In other words both C# and Java compile initially to an intermediate
language. In the case of C# it is called as Microsoft Intermediate Language (MSIL), and in the case of Java it is known as Java bytecode. In each case the intermediate language is run by
interpretation or just-in-time compilation - on an appropriate 'virtual machine'. Java bytecode is run by JVM and the MSIL is run by the CLR.
Both languages have powerful reflection capabilities.
- Both of these support only OOP and all classes descend from the Object class.
- Both of these have garbage Collection coupled with the elimination of pointers. In C#, however, restricted use of pointers is permitted within code marked as unsafe.
- Unlike C or C++, in Java and C# there are no header files. All code is scoped to packages(Java) or assemblies(C#).
- Unlike C++ there is no problem in declaring one class before another with circular dependencies.
- Objects must be allocated on the heap with the use of the new keyword.
- Both support concurrency through thread support by putting a lock on objects when entering code marked as locked / synchronized.
- Both have single inheritance and support for interfaces.
- There are no global functions or constants, everything belongs to a class.
- There is built-in bounds checking of Arrays and strings.
- The "." operator is always used and there are no more -> and :: operators.
- All values must be initialized before use.
- Integers cannot be used as boolean.
- In both languages try blocks can have a finally clause.