C# for C++ Developers
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

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.


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).



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.


The long type: In C#, the long type is 64 bits, while in C++, it is 32 bits.


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.



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.


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


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.


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.


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 and exception is thrown.



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

& Operator

| Operator

^ Operator



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.


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.


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.


Overloading operators is performed differently in C#.

C# Operators

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



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

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


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.


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, although there are significant differences.

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).

© 2016 Microsoft