Comparison Between C++ and C#
The following table contains important comparisons between C++ and C# features. If you are a C++ programmer, this table will give you the most important differences between the two languages at a glance. For details, use the link to browse to the detailed topic.
Note C++ and C# projects are derived from different project models. For information on the differences between C++ and C# projects, see Item Management in Projects and Using Solution Explorer.
| Feature | Refer to the topic |
|---|---|
| Inheritance: A class can inherit implementation from one base class only. Also a class or an interface can implement multiple interfaces. | class |
| Arrays: The syntax of declaring C# arrays is different from that of C++ arrays. The tokens "[]" appear following the array type in C#. | Arrays |
| The bool type: There is no conversion between the bool type and other types (specifically int). | bool |
| The long type: In C#, the long data type is 64 bits, while in C++, it is 32 bits. | long |
| The struct type: In C#, classes and structs are semantically different. A struct is a value type, while a class is a reference type. | struct |
| The switch statement: Unlike the C++ switch statement, C# does not support fall through from one case label to another. | switch |
| The delegate type: Delegates are roughly similar to function pointers in C++, but they are type-safe and secure. | delegate |
| Calling the overridden base class members from derived classes. | base See also the examples for override |
| Using the new modifier to explicitly hide an inherited member. | new |
| Declaring override methods requires the override keyword. | override |
| Preprocessor directives are used for conditional compilation. No header files are used in C#. | C# Preprocessor Directives |
| Exception handling: Using the finally statement. | try-finally |
| C# operators: C# supports additional operators such as is and typeof. It also introduces different functionality of some logical operators. | & Operator |
| Use of the extern keyword. | extern |
| Use of the static keyword. | static |
| An alternative way to the C++ initialization list in constructing the base class. | See the examples for virtual |
| The general structure of the C# program: namespaces, classes, structs, delegates, and enumerations. | General Structure of a C# Program |
| The Main method is declared differently from the main function in C++. Also, compare the usage of the command-line arguments for each language. | Main |
| Method parameters: C# supports ref and out parameters, which are used instead of pointers in passing parameters by reference. | ref out |
| Pointers are allowed in C# but only in unsafe mode. | unsafe |
| Overloading operators is performed differently in C#. | C# Operators |
| Strings: C# strings are different from C++ strings. | string |
| The foreach keyword allows you to iterate through arrays and collections. | foreach, in |
| No global methods or variables in C#: Methods and variables must be contained within a type declaration (such as class or struct). | General Structure of a C# Program |
| No header files or #include directives in C#: The using directive is used to reference types in other namespaces without fully qualifying the type names. | using |
| Local variables in C# cannot be used before they are initialized. | 5. Variables |
| Destructors: In C#, you don't have control over when a destructor is called because destructors are called automatically by the garbage collector. | Destructors |
| Constructors: Similar to C++, if you don't 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 |
| 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 |
| In C#, method parameters cannot have default values. Use method overloads if you want to achieve the same effect. | Compiler Error CS0241 |
For comparisons between C# and other programming languages, see the Language Equivalents documentation.
See Also
C# Language Tour | Item Management in Projects | Using Solution Explorer