C# for C++ Developers

The following table contains important comparisons between C# and native C++, which does not use /clr. If you are a C++ programmer, this table will give you the most important differences between the two languages at a glance.

Note

C++ and C# projects are derived from different project models. For more information about the differences between C++ and C# projects, see Item Management in Projects and Using Solution Explorer.

Feature

Refer to the topic

Inheritance: In C++, classes and structs are virtually identical whereas in C#, they are quite different. C# classes can implement any number of interfaces, but can inherit from only one base class. Furthermore, C# structs do not support inheritance, and do not support explicit default constructors (one is provided by default).

class

interface

struct (C# Reference)

Arrays: In C++ an array is merely a pointer. In C#, arrays are objects that include methods and properties. For example, the size of an array can be queried via the Length property. C# arrays also employ indexers that verify each index used to access the array. The syntax for declaring C# arrays is different from that for C++ arrays: the tokens "[]" appear following the array type in C#, not the variable.

Arrays (C# Programming Guide)

Indexers (C# Programming Guide)

Booleans: In C++, the bool type is essentially an integer. In C#, there is no conversion between the bool type and other types.

bool

The long type: In C#, the long type is 64 bits, while in Microsoft C++, it is 32 bits. For more information, see Fundamental Types (C+).

long

Passing parameters: In C++, all variables are passed by value unless explicitly passed with a pointer or a reference. In C#, classes are passed by reference and structs are passed by value unless explicitly passed by reference with the ref or out parameter modifiers.

struct

class

ref (C# Reference)

out (C# Reference)

The switch statement: Unlike the C++ switch statement, C# does not support fall-through from one case label to another.

switch

Delegates: C# delegates are roughly similar to function pointers in C++, are type-safe and secure.

delegate

Base-class methods: C# supports the base keyword for calling the overridden base class members from derived classes. Also, overriding virtual or abstract methods is explicit in C#, using the override keyword.

base

See also the examples for override

Method hiding: C++ supports the implicit "hiding" of method through inheritance. In C#, you must use the new modifier to explicitly hide an inherited members.

new

Preprocessor directives are used for conditional compilation. No header files are used in C#.

C# Preprocessor Directives

Exception handling: C# provides the finally keyword to provide for code that should be executed regardless of whether an exception is thrown.

try-finally

try-catch-finally

C# operators: C# supports additional operators such as is and typeof. It also introduces different functionality for some logical operators.

& Operator

| Operator

^ Operator

is

typeof

The typedef keyword. In C++, typedef is used to create shorter or more convenient names for types that have already been declared. In C#, the using directive provides this capability.

using Directive (C# Reference)

The extern keyword: In C++, extern is used to import types. In C#, extern is used to create aliases for using different versions of the same assembly.

extern

The static keyword: In C++, static can be used both to declare class-level entities and to declare types that are specific to a module. In C#, static is only used to declare class-level entities.

static

The Main method in C# is declared differently from the main function in C++. In C# it is capitalized, and always static. Also, support for processing of command-line arguments is much more robust in C#.

Main() and Command-Line Arguments (C# Programming Guide)

Pointers are allowed in C#, but only in unsafe mode.

unsafe

Overloading operators is performed differently in C#.

C# Operators

Strings: In C++ a string is simply an array of characters. In C#, strings are objects that support robust searching methods.

string

String

The foreach keyword enables you to iterate through arrays and collections.

foreach, in

Globals: In C#, global methods and variables are not supported. Methods and variables must be contained within a class or struct.

General Structure of a C# Program

The #define preprocessing directive: In C++ the #define directive is commonly used to declare constant values. In C# the #define directive cannot be used for this purpose. Constants in C# are best defined as enumerated types (integral values only) or as static members of a class or struct. If you have several such constants, consider creating a separate "Constants" class to hold them.

static (C# Reference)

const (C# Reference)

enum (C# Reference)

Importing types: In C++, types common to multiple modules are placed in header files. In C#, this information is available via metadata.

using

Metadata Overview

Local variables in C# cannot be used before they are initialized.

Methods (C# Programming Guide)

Memory management: C++ is not a garbage collected language; memory that is not explicitly release remains allocated until the process terminates. C# is a garbage collected language.

Garbage Collection

Destructors: C# has different syntax for deterministically releasing unmanaged resources.

Destructors

using Statement (C# Reference)

Constructors: Similar to C++, if you do not provide a class constructor in C#, a default constructor is automatically generated for you. The default constructor initializes all the fields to their default values.

Instance Constructors

Default Values Table

C# does not support bit fields.

C++ Bit Fields

C# input/output services and formatting rely on the run-time library of the .NET Framework.

C# Language Tour

Formatting Numeric Results Table

In C#, method parameters cannot have default values. Use method overloads if you want to achieve the same effect.

Compiler Error CS0241

In C#, generic types and methods provide for type parameterization in a way that is similar to C++ templates. There are significant differences, however. For example, in C# generic type information is preserved at run time.

Generics in C#

The as keyword is similar to a standard cast, except that rather than throw an exception if the conversion fails, the return value is null. This is similar to using static_cast in C++, which, unlike dynamic_cast, performs no run-time check and hence does not throw an exception on failure.

as (C# Reference)

For more information about comparisons between keywords in C# and other programming languages, see Language Equivalents. For information on the general structure of C# applications, see General Structure of a C# Program (C# Programming Guide).

See Also

Concepts

C# Programming Guide

Item Management in Projects

Using Solution Explorer

Reference

Fundamental Types (C+)