C# Compared to Other Languages

C# is a modern, component-orientated language with many features in common with other .NET Framework programming languages. C# has only about 80 keywords, most of which will be familiar to anyone who has worked with C, C++, Java or Visual Basic. There are syntax differences, but they are generally minor.

Learning C# is made easier by the Visual C# Express Edition editing environment which uses IntelliSense to do a lot of the hard work for you. The C# editor automatically keeps your code tidy, suggests methods and other properties as you need them, and highlights potential errors as you type.

Hello, World!

To give you a quick idea what a C# program looks like, here is the famous "Hello, World!" application in several different languages:

// C# Hello, World! 
using System;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }

// C++ Hello, World!
#include <iostream>
using namespace std;
static int main()
{
  cout << "Hello World!" << endl;
  return 0;
}

' Visual Basic Hello, World!
Module Module1

    Sub Main()
        Console.WriteLine("Hello, World!")
    End Sub

End Module

// Java Hello, World!
class HelloWorldApp
{
    public static void main(String[] args)
    {
        System.out.println("Hello, World!");
    }
}

For programmers experienced with other languages, C#'s most important differences are listed in the following sections.

C# Compared to C and C++

  • Memory is managed with a garbage collection system: there is no delete method to undo a call to new.

  • Header (.h) files are not used, or required. The information stored in header files is now stored as part of an assembly.

  • In a C# program, no semicolons are required after closing braces in a class definition.

  • The Main method is capitalized, and is a member of a static class. Main returns int or void.

  • Every program must have a Main method, or it will not compile.

  • The switch statement's break statement is not optional.

  • Conditions must be Boolean.

  • Default values are assigned by the compiler (null for reference types, 0 for value types).

C# Compared to Visual Basic

  • Semicolons are used instead of line breaks.

  • C# is case-sensitive, for example, the Main method is capitalized.

  • Conditions must be Boolean.

C# Compared to Java

  • The Main method is capitalized.

  • Boxing and unboxing convert between value and reference types: there is no need to create wrapper types.

  • A final class in Java is a sealed class in C#.

  • C# supports properties.

  • C# methods are non-virtual by default.

  • C# supports attributes for including extra information for the compiler.

See Also

Tasks

How to: Build a C# Application in 60 Seconds

Concepts

Visual C# Express Features

C# and the .NET Framework

Other Resources

Visual C# Express

Getting Started with Visual C# Express