Negative tive C # Use the AS and is operators for type conversion

Source: Internet
Author: User

InProgramIs common. InC #Supports the type conversion method of the previous language, that is, use the type name for forced conversion, for example:

ObjectObjtest =NewNewtype ();

Newtype newvalue = (newtype) objtest;

 

However, a serious problem is thatObjtestConvertNewtypeThis process is not safe when you type the object, so you need to useTry-catchStatement. In this way, it is saferCodeThe method is as follows.

ObjectObjtest =NewNewtype ();

Newtype newvalue =Null;

Try

{

Newvalue = (newtype) objtest;

}

Catch(Exception ERR)

{

MessageBox. Show (ERR. Message );

}

 

HoweverC #Is outdated and inefficient. InC #Is more efficient and secure.AsOperator, the correct code to complete the above similar functions should be as follows.

ObjectObjtest =NewNewtype ();

Newtype newvalue = objtestAsNewtype;

 

Obviously, the above method looks simple first, at least the number of codes is the same as that of the strong conversion method.For securityAsFor operators, it does not perform conversion operations. When the type of the conversion object belongs to the conversion target type or the conversion target type is derived, this conversion operation can be successful, and no new objects are generated.. ThereforeAsTo perform type conversion to ensure security. Why?AsThe efficiency of type conversion is higher than that of the old type conversion, becauseAsAs mentioned above, the operator first checks the type of the current object and converts the object only when the type meets the requirements. The traditional type conversion method is to directly convert the current object, and to ensure the conversion is successful, addTry-catchThis determines that its efficiency will not be higherAsOperator efficiency.

 

Pay attention to the use of traditional methods orAsBefore using the type conversion operator, you must determine whether the conversion is successful, as shown below:

If(Newvalue! =Null)

{

// Work with the object named "newvalue"

}

 

HoweverAsThe operator has the following restrictions.

The first one is that there is no need to convert the types between types, that is, the compilation error will occur as follows.

Newtype newvalue =NewNewtype ();

Newtype1 newvalue = newvalueAsNewtype1;

 

The second is that it cannot be applied to value-type data, that is, it cannot be written as follows (compilation errors may also occur ).

ObjectObjtest = 11;

IntNvalue = objtestAs Int;

 

For the first point, you can use the traditional type conversion method, but the following method is not correct.

Newtypeone newtestone =NewNewtypeone ();

Newtypetwo newtesttwo = (newtypetwo) newtestone;

 

However, writing just above cannot be completed. To make the above operations complete correctly, you need to add the type conversion operator function to the original type, that is, you need to complete code similar to the following.

Public ClassNewtypeone

{

Public StaticExplicit OperatorNewtypetwo (newtypeone objtest)

{

// Convert object into New Type

}

}

 

For the second pointC #Available inIsOperators, coupled with the old type conversion operation, can safely complete the conversion, so to complete the above operation, the correct syntax is as follows.

ObjectObjtest = 11;

If(ObjtestIs Int)

{

IntNvalue = (Int) Objtest;

}

 

Besides the preceding two restrictionsAsOperator to perform type conversion, there is a subtle problem. As mentioned aboveAsThe object type to be converted must be the target type or the derived type of the conversion target type.AsYou cannot convert an operator to a correct type during type conversion. That is to say, you should convert it to a child type but convert it to a parent type. But I don't think this is a serious problem because it is usedAsWhen you use an operator to perform type conversion, it is very clear how to select the target type during encoding, that is, the parent type is used as the target type, the purpose of type conversion is to convert the data to the parent type object for operation, and vice versa.

 

InC #A good type conversion method has been provided, so you can select the type conversion method as follows.

type conversion

select

/TD>

Object => known reference types

use the as operator to complete

Object => known value type

use the is operator to determine the type, and then use the type forced conversion method to convert

first, you need to provide the conversion function for the corresponding type, and then use the type conversion method

it is best to use the static method

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.