C ++ vs C # (3): Switch, type conversion

Source: Internet
Author: User
Tags visual studio 2010

// ================================================ ====================================
// 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. To put it simply, although the following statement is normal in C ++, it cannot be compiled in C:Switch (iselect) <br/>{< br/> case 1: <br/> ival = 1; <br/> break; <br/> case 2: <br/> ival = 2; <br/> case 3: <br/> iend = 3; <br/> break; <br/>}

 

C # will prompt error: Error cs0163: control cannot fall through from one case label ('case 2: ') to another. 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 of course not. In fact, there is no statement between "Case 2:" and "Case 3:Switch (iselect) <br/>{< br/> case 1: <br/> ival = 1; <br/> break; <br/> case 2: <br/> case 3: <br/> iend = 3; <br/> break; <br/>}

 

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 to the switch is that the condition can use strings, for example:Switch (strval) <br/>{< br/> case "A": <br/> break; <br/> case "B ": <br/> case "C": <br/> break; <br/>}

 

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 confusing. Let's take a look at this Code:Byte nval = 0; <br/> int ival = 2000; <br/> 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 following 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: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, and you can add checked before the conversion to perform the check, for example: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:
String strval = "22"; <br/> byte nval = 0; <br/> // nval = (byte) strval; // compilation failed <br/> 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, the following code segment is available:String strval = "G"; <br/> byte nval = 0; <br/> nval = convert. tobyte (strval );

After convert is executed, an error is prompted,
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.