Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C# Strikes a Chord


C# Strikes a Chord

C# Strikes a Chord

Syntax Comparison


Table 1 summarizes the comparison between C++, Java and C#. We have deliberately put C# in the middle column flanked by C++ and Java to make it easier to see in what direction C# leans with regard to each feature. The Executable Environment and Language APIs are also included in the comparison. These are as important as the language's syntax in determining how the programming language gets used. In fact, this has always been true with languages. The choices between interpreted versus common .obj files and linking options have determined the flexibility and critical runtime performance of a number of languages. In addition, the availability of a common set of subroutine libraries and system APIs have been a selling point for languages from Fortran to C to Smalltalk and now Java and C#.

The first thing you note is that similarities between C++, Java and C# go well beyond HelloWorld. The number of common or synonymous keywords, operators and flow of control statements never drops below 75 percent. And why not? Pascal derived much syntax from Algol, and of course C++ from C. That programming language designers are following Isaac Newton's suggestion and deriving their syntax from a common starting base is no small boon to programmers. Already pressed with unremitting change in hardware and software technology, anything that helps to keep a language familiar and easily learned is welcome.

There may seem to be some anomalies among the 75-percent-common syntax features. Even though C# has only 52 operators, because C++ has about a dozen keyword equivalents to character operators (such as bitand == "&" and bitor == "|") it has a number of duplicate operators matching C# equivalents. The result is that 59 C++ operators map into some (but not all) of the 52 C# operators. Note also we have called these "common or synonymous". Thus we consider C#'s is operator to be substantially synonymous with Java's instanceof. Perhaps the best approach is to highlight the notable differences in syntax between the two languages.

 

Table 1
Comparison of C++ and Java to C# 

  Syntax Features

C++

C# Java
  Program code to intermediate code No Yes Yes
  Single inheritance, all objects implicitly derived from master class, Object No Yes Yes
  Unicode for char, String, identifiers No Yes Yes
  Uniform implementation of primitive type bitsizes No Yes Yes
  All primitive types derived from Object No Yes No
  Allows pointers, explicit memory mgmt. Yes Yes Simple, =null
  Struct, enum Yes Yes No
  Common or synonymous keywords to C# 56 of 74 69 of 69 46 of 50
  Flow of control statements 9 7*
  Common or synonymous operators to C# 59 of 70 52 of 52 47 of 56
  Explicit exception handling with throw, try-finally-catch  Yes, no finally Yes Yes
  Operator overloading Yes Yes except for = No
  Preprocessor statements (#define etc) Yes Yes No
  In-processing attributes No Yes No
  Parameter options in, out, ref, params No Yes No
  Compiler aids/hints asm, inline, register Yes No No
  Templates, generic programming Yes No No
  Executable Environment
  Open, cross-platform intermediate language code No Promised Yes
  Garbage collection and memory management No Yes Yes
  Type safe assignments, method invocation, initializations, array bounds checks Some Yes Yes
  Explicit invocation sequence for applets, servlets, beans/components No No Yes
  Fine-grained security of access/operation No Yes Yes
  Disassembly and metadata sharing Some Yes Yes
  JIT-Just In Time compilation and performance enhancers No Yes Yes
 Language API
  Number of standard classes and methods 100's 100's 1000's
  Open source API Some To be decided All
  Standard container, math, I/O classes, etc Yes Yes Yes
  Direct tie to XML for documentation No Yes Yes
  Direct use of XML for interface and remote procedure calls No Yes No

* 7 are identical: no foreach, goto

Next: Notable Differences

1 | 2 | 3 | 4 | 5 | 6


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.