C # Coercion Type conversions

Source: Internet
Author: User

Reference

1, int is suitable for conversion between simple data types, the default integral type of C # is int32 (bool type is not supported);

2, Int. Parse (String sparameter) is a static method, and parameter types only support string types ;

3. Convert.ToInt32 () is suitable for converting object type to int type;

4. Nuances of Convert.ToInt32 () and Int.parse ():

For null (NULL) processing, CONVERT.TOINT32 (NULL) returns 0 without generating any exceptions, but int. Parse (NULL) produces an exception.

For example:

If we take the value of a parameter page from the URL, we know that the value is an int, so we can use Convert.ToInt32 (request.querystring["page") or int. Parse (request.querystring["page"]), but if the page parameter does not exist in the URL, then the former will return 0,0 may be a valid value, so you do not know that the URL is not in the original there is no such parameter and continue the next processing, This can have unintended effects, and in the latter case there is no page this argument throws an exception, we can catch the exception and then do the corresponding processing, such as prompting the user for missing parameters, rather than the parameter value as a limit processing.

5. Another difference is:

(1). Convert.ToInt32 (double value) if value is a number in the middle of two integers, returns the even of the two, that is, 4.5 is converted to 4, and 5.5 is converted to 6;

(2). int (4.6) = 4,int conversion Other numeric type is int without rounding, casting (intercepting integer part);

(3). Int. Parse (4.5) will directly error: "The input string is malformed."

Int. Parse is to convert a string to int;

Convert.ToInt32 is the conversion of objects that inherit from object to int; you get an object that you want to convert to int, with Int. It is not possible to parse, but to use Convert.ToInt32.

Summarize:

(1) Convert.ToInt32 parameters are more, Int.parse can only convert string types.

(2) The parse is to convert a string into a int,char,double .... And so on, that is *. Parse (string) must be a string in parentheses.

(3) Convert can provide multiple types of conversions, that is, convert.* () can be of many types (including string) in parentheses.

As Operation Method:

As for security, for the as operator, it does not have a conversion operation, and when the type of the object that needs to be converted belongs to the transformation target type or to the derived type of the target type, the conversion operation succeeds and does not produce a new object.

It is therefore safe to use as for type conversion. Why type conversion with the AS operator is more efficient than the old-fashioned type conversion, because the as operator is converted as previously stated, the type of the current object is judged first, and the type is converted when it satisfies the requirements.

The traditional type conversion method is to use the current object to transform directly, and in order to protect the conversion success, to add Try-catch, which determines that its efficiency is not higher than the efficiency of the as operator.

When using as for type conversion, the object type to be converted must be the target type or the derived type of the target type, then there is a problem that the type conversion with the AS operator cannot be converted to the correct type, that is, it should have been converted to a subtype, but converted to a parent type. But I do not think this is a serious problem, because when using the as operator for type conversion, how to choose the target type when encoding is very clear, that is, with the parent type as the target type, then the purpose of the type conversion is to convert to the parent type object to operate, and vice versa.

Type conversions Use selection

Object conversion Known reference type is done using the as operator
The Object conversion known value type is first judged using the IS operator, and then converted with a type-strong-turn method
Conversions between well-known reference types first require the corresponding type to provide the conversion function, and then convert it with a type-strong-turn method
Conversion between known value types is best used by the static methods that are involved in the system-provided convert class

C # Coercion Type conversions

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.