日期:2013-06-15  浏览次数:20464 次

 

C# FAQ for C++ programmers

1. What is C#?[Introduction]

C# is a programming language designed by Microsoft. It is loosely based on C/C++, and bears a striking similarity to Java. Microsoft describe C# as follows:
"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++."

2. How do I develop C# apps? [Introduction]

The (free) .NET SDK contains the C# command-line compiler (csc.exe). Visual Studio has fully integrated support for C# development. On Linux you can use Mono.

3. Does C# replace C++?[Introduction]

There are three options open to the Windows developer from a C++ background:
----Stick with standard C++. Don't use .NET at all.
----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.
----Forget C++ and use C#.
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.

4. Does C# have its own class library? [Introduction]

Not exactly. The .NET Framework has a comprehensive class library, which C# can make use of. C# does not have its own class library.

5. What standard types does C# use?[Types]

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++.
Furthermore,these simple built-in types are just aliases to the according .Net Framework classes.For instance,bool for System.Boolean ,char for System.Char,int for System.Int32,float for System.Single,double for System.Double and long for System.Int64

6. Is it true that all C# types derive from a common base class? [Types]

Yes and no. All types can be treated as if they derive from object (System.Objec