FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
December 01, 2000

C# Strikes a Chord

(Page 2 of 6)

C# Strikes a Chord

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
Hello World in C++, C#, and Java

 
  #include <iostream.h>
// This is a comment in C++ code

int main(){
 for(int ii = 1; ii <= 100; ii++)
     cout <<"Hello World, repeated another " << ii << " times. " << '\n';
}
   


 
 

// This is a comment in C# - C Sharp code

using System;
class HelloWorld{
  static void Main(){
   for(int ii = 1; ii <= 100; ii++)
     Console.WriteLine("Hello World, repeated another {0} times. ", ii);
  }
}


 
  // 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

1 | 2 | 3 | 4 | 5 | 6

Previous Page | 1 | 2 | 3 | 4 | 5 | 6 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK