// ================================================ ====================================
// 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, view plaincopy to clipboardprint?
Enum Format
{
RGB888,
RGB565,
};
Enum Format
{
RGB888,
RGB565,
};
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 as view plaincopy to clipboardprint?
// C ++
Format format = RGB888;
// C #
Format format = Format. RGB888;
// C ++
Format format = RGB888;
// C #
Format format = Format. RGB888;
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, view plaincopy to clipboardprint?
Enum Format
{
RGB888,
RGB565,
};
Enum Format2
{
RGB888,
RGB565,
}
Enum Format
{
RGB888,
RGB565,
};
Enum Format2
{
RGB888,
RGB565,
}
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 as: view plaincopy to clipboardprint?
String strFormat = Format. RGB888.ToString ();
String strFormat = Format. RGB888.ToString ();
You can use not only member functions, but also global Convert functions, such as view plaincopy to clipboardprint?
String strFormat = Convert. ToString (Format. RGB888 );
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, view plaincopy to clipboardprint?
Format format = (Format) Enum. Parse (typeof (Format), "RGB888 ");
Format 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. If public is to be specified, each member variable must be specified, for example, view plaincopy to clipboardprint?
Struct Size
{
Public int x;
Public int y;
};
Struct Size
{
Public int x;
Public int y;
};
Maybe a friend who is familiar with C ++ may think it is a little troublesome to write this code. Can C # Be like C ++: view plaincopy to clipboardprint?
Struct Size
{
Public:
Int x;
Int y;
};
Struct Size
{
Public:
Int x;
Int y;
};
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.
This article from the CSDN blog, reprinted please indicate the source: aspx "> http://blog.csdn.net/norains/archive/2010/12/10/6067276.aspx