// ================================================ ====================================
// TITLE:
// C ++ vs c # (3): switch, type conversion
// AUTHOR:
// Norains
// DATE:
// Tuesday 1-December-2010
// Environment:
// Visual Studio 2010
// Visual Studio 2005
// ================================================ ====================================
1. switch
Both C ++ and C # Have the switch keyword, but there is a difference between the two. The biggest difference is that in C #, if there is a statement after case, the switch must be jumped out with a break. Simply put, although the following statement is normal in C ++, it cannot be compiled in C #: view plaincopy to clipboardprint?
Switch (iSelect)
{
Case 1:
IVal = 1;
Break;
Case 2:
IVal = 2;
Case 3:
IEnd = 3;
Break;
}
Switch (iSelect)
{
Case 1:
IVal = 1;
Break;
Case 2:
IVal = 2;
Case 3:
IEnd = 3;
Break;
}
Error CS0163: Control cannot fall through from one case label (case 2 :) to another will be prompted in C. That is to say, "case 2:" cannot be crossed to "case 3 :". So, does it mean that "case 2:" and "case 3:" cannot execute the same code? The answer is no. In fact, there is no statement between "case 2:" and "case 3:": view plaincopy to clipboardprint?
Switch (iSelect)
{
Case 1:
IVal = 1;
Break;
Case 2:
Case 3:
IEnd = 3;
Break;
}
Switch (iSelect)
{
Case 1:
IVal = 1;
Break;
Case 2:
Case 3:
IEnd = 3;
Break;
}
The changed code can be smoothly compiled in C. Through this example, we can know that the switch is highly flexible in C ++ and is highly secure in C #-after all, there are still many people who forget to write break.
C # Another improvement for the switch is that the condition can use a string, for example, view plaincopy to clipboardprint?
Switch (strVal)
{
Case "":
Break;
Case "B ":
Case "C ":
Break;
}
Switch (strVal)
{
Case "":
Break;
Case "B ":
Case "C ":
Break;
}
This code segment cannot be compiled in C ++, but everything is normal in C #, because in C ++, the condition must be of the numerical type. This is also impossible. The string is a standard built-in type in C #, but it is a class derived from STL in C ++.
2. type conversion
Both languages have two types of conversions: implicit conversion and explicit conversion. Note that in C #, values that can be implicitly converted are lossless. This may be a bit tricky. Let's take a look at this code: view plaincopy to clipboardprint?
Byte nVal = 0;
Int ivals = 2000;
NVal = iVal;
Byte nVal = 0;
Int ivals = 2000;
NVal = iVal;
This code has an implicit conversion when assigning values to nVal, that is, converting int to byte, unless the value of iVal is 0 ~ 255, otherwise the precision will be lost. In C ++, the code can be compiled. However, in C #, the Code fails with the error: error CS0266: Cannot implicitly convert type int to byte. an explicit conversion exists (are you missing a cast ?).
If you want to assign the int type to byte, explicit conversion must be performed in C #, for example, view plaincopy to clipboardprint?
NVal = (byte) iVal;
NVal = (byte) iVal;
At this time, the performance of C # is the same as that of C ++. Even if the precision is lost, the conversion will be completed. However, C # still has a hand left. You can add checked before the conversion to check it, for example, view plaincopy to clipboardprint?
NVal = checked (byte) iVal );
NVal = checked (byte) iVal );
When the actual value of iVal is within the permitted byte range, everything is fine. However, if the range is exceeded, a prompt dialog box is displayed to show you data overflow. For example:
For C ++, this transformation in the form of parentheses is called the old method and is not recommended for programmers. Replace static_cast, reinterpret_cast, const_cast, and dynamic_cast. I think the most difficult thing to implement for compilers, especially in the field of embedded systems, is dynamic_cast. The most typical example is that when you use RVDS, the Code with dynamic_cast cannot be compiled, but it is smooth sailing in MDK.
So C #, is there a similar XXX_cast? The answer is no. C # Another way is to use Convert objects. There are many methods for this object, such as. ToByte and ToInt16. So why is there a bracket conversion and an object function? The reason is simple. The bracket conversion belongs to C #, and the object function belongs to. Net Framework. In other words, the object function method is not only suitable for C #, but also for VB and all languages running on. Net Framework. In addition, there is an important difference between the two methods. The bracket conversion method can only be used in numeric values, but the object function can be used in most types, for example:
View plaincopy to clipboardprint?
String strVal = "22 ";
Byte nVal = 0;
// NVal = (byte) strVal; // compilation fails
NVal = Convert. ToByte (strVal); // compiled smoothly
String strVal = "22 ";
Byte nVal = 0;
// NVal = (byte) strVal; // compilation fails
NVal = Convert. ToByte (strVal); // compiled smoothly
However, to convert a string to a value, note that the content of the string must conform to the value format. Otherwise, an error occurs. For example, there is a code snippet: view plaincopy to clipboardprint?
String strVal = "G ";
Byte nVal = 0;
NVal = Convert. ToByte (strVal );
String strVal = "G ";
Byte nVal = 0;
NVal = Convert. ToByte (strVal );
After Convert is executed, an error is prompted,