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.
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).
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.
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 Microsoft C++, it is 32 bits. For more information, see Fundamental Types (C++).
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.
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#.
Exception handling: C# provides the finally keyword to provide for code that should be executed regardless of whether an exception is thrown.
C# operators: C# supports additional operators such as is and typeof. It also introduces different functionality for some logical operators.
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.
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#.
Pointers are allowed in C#, but only in unsafe mode.
Overloading operators is performed differently in C#.
Strings: In C++ a string is simply an array of characters. In C#, strings are objects that support robust searching methods.
The foreach keyword enables you to iterate through arrays and collections.
Globals: In C#, global methods and variables are not supported. Methods and variables must be contained within a class or struct.
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.
Importing types: In C++, types common to multiple modules are placed in header files. In C#, this information is available via metadata.
Local variables in C# cannot be used before they are initialized.
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.
Destructors: C# has different syntax for deterministically releasing unmanaged resources.
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.
C# does not support bit fields.
C# input/output services and formatting rely on the run-time library of the .NET Framework.
In C#, method parameters cannot have default values. Use method overloads if you want to achieve the same effect.
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.
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.
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).