// ================================================ ====================================
// Title:
// C ++ vs C # (4): enumeration, struct
// Author:
// Norains
// Date:
// Tuesday 7-December-2010
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================
1. Enumeration
Both C ++ and C # Use Enum to declare an enumeration type, for example:Enum format <br/>{< br/> rgb888, <br/> rgb565, <br/> };
However, they are quite different in terms of use. For C ++, you can directly use the defined type, while C # must add a. identifier, such:// C ++ <br/> format = rgb888; </P> <p> // C # <br/> format = format. rgb888; <br/>
In this case, C # seems to be cumbersome and requires corresponding Type Definitions. However, this brings another benefit: Different Enum types in the same namespace can have the same value, for example:Enum format <br/>{< br/> rgb888, <br/> rgb565, <br/> }; </P> <p> Enum format2 <br/> {<br/> rgb888, <br/> rgb565, <br/>}< br/>
This code can be compiled smoothly in C #. the following error is prompted in C ++: Error c2365: 'rgb888': redefinition; previous definition was 'enumerator };
In C ++, Enum is essentially a set of numerical values, and its name is only the identification of numerical values. However, for C #, it is a step forward, enum can also obtain the corresponding name string, such:String strformat = format. rgb888.tostring ();
You can use not only member functions, but also global convert functions, such:String strformat = convert. tostring (format. rgb888 );
More interestingly, in addition to the numeric type, the string type can also be converted to the enum type, for example:Format = (Format) enum. parse (typeof (format), "rgb888 ");
Note that the string here is also case sensitive. If the converted string is not in the enum type name column, an error occurs.
2. struct
In C ++, the class and struct are consistent. The only difference is that the default access domain of the class is private, while the class is public. However, for C #, the default access domain of struct members is private. To specify public, each member variable must be specified, for example:Struct size <br/>{< br/> Public int X; <br/> Public int y; <br/> };
If you are familiar with C ++, you may think it is a little troublesome to write this code. Can C # Be like C ++:Struct size <br/>{< br/> Public: <br/> int X; <br/> int y; <br/> };
It is a pity that this writing method is not true in C. Simply remember, C #'s structure syntax can be fully transplanted to C ++, and vice versa.