C# Strikes a Chord
Code Comparison
So C++, Java, and C# are all defined as general purpose object-oriented languages. Superficially the languages look very much alike because they use the same variable naming conventions, comment structure, and code block delimiters, "{}". Our HelloWorld programs shown in Example 1 exhibit these surface similarities, but there are also some notable differences.
First, C#'s Console I/O and data formatting changes follow neither the C++ nor Java models. From RPG thru Cobol, Fortran, Basic, C, C++, Visual Basic, Java, and now C#, programmers have come to expect with each new language that they must relearn the simplest of tasks character input and output. Unfortunately, C# did not break with this venerable programming tradition.
Second and perhaps of even more importance, the basic structure of C++ differs from C# and Java. The latter two languages are class based. You cannot create a program in either C# or Java without at least declaring one class HelloWorld in this case. Moreover, this class, HelloWorld, implicitly derives from the same root class, Object. Again, Java and C# follow the same single-inheritance design strategy, as opposed to the multiple inheritance allowed in C++. This is not the only area in which C# follows Java much more closely than C++. But as we shall soon see, C# also reinstates some C++ features that James Gosling trimmed out of the compact, run-anywhere Oak that became Java.
And finally, C# has its own distinctive features. Anders Hejlsberg angled for a programming language with greater interoperability with other components and languages as a top design goal. So, let's compare the languages.
Example
1 |
||
#include
<iostream.h> // This is a comment in C++ code
int main(){ |
||
// This is a comment
in C# - C Sharp code |
||
// This
is a comment in Java code class HelloWorld{ public static void main(String[] args){ for(int ii= 1; ii <= 100; ii++) System.out.println("Hello World repeated another " + ii + " times."); } } |
Next: Syntax Comparison