"C# is a simple, modern, object oriented, and type-safe programming language derived from C
and C++. C# (pronounced 'C sharp') is firmly planted in the C and C++ family tree of languages,
and will immediately be familiar to C and C++ programmers. C# aims to combine the high
productivity of Visual Basic and the raw power of C++."
Use C++ with .NET. Microsoft supply a .NET C++ compiler that produces IL rather than
machine code. However to make full use of the .NET environment (e.g. garbage collection), a
set of extensions are required to standard C++. In .NET 1.x this extended language is called
Managed Extensions for C++. In .NET 2.0 ME C++ has been completely redesigned under the
stewardship of Stan Lippman, and renamed C++/CLI.
Each of these options has merits, depending on the developer and the application. For my own
part, I intend to use C# where possible, falling back to C++ only where necessary. ME C++ (soon
to be C++/CLI) is very useful for interop between new .NET code and old C++ code - simply write
a managed wrapper class using ME C++, then use the managed class from C#. From experience,
this works well.
C# supports a very similar range of basic types to C++, including int, long, float, double, char,
string, arrays, structs and classes. However, don't assume too much. The names may be familiar,
but many of the details are different. For example, a long is 64 bits in C#, whereas in C++ the size
of a long depends on the platform (typically 32 bits on a 32-bit platform, 64 bits on a 64-bit
platform). Also classes and structs are almost the same in C++ - this is not true for C#. Finally,
chars and strings in .NET are 16-bit (Unicode/UTF-16), not 8-bit like C++.
Yes and no. All types can be treated as if they derive fromobject (System.Object), but in order to
treat an instance of a value type (e.g. int, float) as object-derived, the instance must be converted
to a reference type using a process called 'boxing'. In theory a developer can forget about this and
let the run-time worry about when the conversion is necessary, but in reality this implicit
conversion can have side-effects that may trip up the unwary.
public static void Main()
The most confusing aspect of this for C++ developers is that C# has predetermined which types are represented as values, and which are represented as references. A C++ developer expects to take responsibility for this decision.
// x1 is a value on the stack
int x2 = new int();
x2 = 3;
It isn't, really. When an int is being used as an int, it is a value. However, when it is being used as an object, it is a reference to an integer value (on the managed heap). In other words, when you treat an int as an object, the runtime automatically converts the int value to an object reference. This process is calledboxing. The conversion involves copying the int to the heap, and creating an object instance which refers to it. Unboxing is the reverse process - the object is converted back to a value.
Not quite. The basic idea is the same, but one significant difference is that C# references can be
null . So you cannot rely on a C# reference pointing to a valid object. In that respect a C#
reference is more like a C++ pointer than a C++ reference. If you try to use a null reference, a
NullReferenceException is thrown.
This action might not be possible to undo. Are you sure you want to continue?