Differences between is and as in C,

Source: Internet
Author: User

Differences between is and as in C,

I. C # type conversion

 

There are two types of conversions in c #: explicit and implicit. The basic rules are as follows:


1. If the base class object is converted to a subclass object, it must be converted explicitly. Rule: (type name) object.
2. The value type and reference type are converted using boxing or unboxing ).
3. Subclass conversion is base class object.


4. The Covent class can be used for conversion between basic types.
5. Use the Parse method to convert the String type to the corresponding basic type. The Parse method can be used for other types except the String type.
6. Use GetType to obtain the exact type of the object.
7. Subclass conversion is base class, and implicit conversion is used.

 

Ii. is in C #

 

Check whether an object is compatible with other specified types and return a Bool value. If an object is of a certain type or its parent type, true is returned. Otherwise, false is returned. Never throw an exception

If the object reference is null, the is Operator always returns false because no object can check its type.

For example

 

C # code Replication
Object o = new object (); if (o is Label) {Label lb = (Label) o; Response. write ("type conversion successful");} else {Response. write ("type conversion failed ");}

 

Iii. as conversion rules in C #
 

1. Check the compatibility of object types and return the conversion result. If the conversion result is not compatible, null is returned;


2. No exception is thrown;


3. If the result is null, an NullReferenceException exception will be thrown during forced type conversion;

4. When using as for type conversion, the object type to be converted must be the target type or the derived type of the conversion target type.

For example

C # code Replication
Object o = new object (); Label lb = o as Label; if (lb = null) {Response. write ("type conversion failed");} else {Response. write ("type conversion successful ");}

 

The as 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 = new NewType ();
NewType1 newValue = newValue as NewType1;
 

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 ).


Object objTest = 11;
Int nValue = objTest as int;

 

Iv. Differences between as and is

 

1. The AS operator in the conversion determines compatibility. If the conversion fails, the as returns null (no new object is generated) instead of an exception. With AS, I don't want to use try-catch to judge type conversion in the future. Therefore, if the as conversion is successful, you must determine whether it is null.


2. AS is a conversion or packing conversion of the reference type. It cannot be converted to a value type. If it is a value type, it can only be combined with is for forced conversion.

3. IS only used for determining type compatibility and does not perform real type conversion. Returns true or false. No null is returned. If the object is null, false is returned.


4. The efficiency of the AS mode IS higher than that of the IS mode. Because the IS mode IS used for type conversion, two type compatibility checks are required. AS only requires one type compatibility and one null check, and the null check is faster than the type compatibility check.
 

 

5. When performing type conversion, you can select the following method:
 

1. Object => known reference type
Use the as operator to complete

2. Object => known value type
Use the is operator to determine the type, and then use the type forced conversion method for conversion.

3. Conversion between known reference types
First, you need to provide the conversion function for the corresponding type, and then use the type conversion method for conversion.

4. Conversion between known value types
It is best to use the static method involved in the Convert class provided by the system

 

6. Differences between (int), Int32.Parse (), and Convert. ToInt32 ()

 

1. (int) Conversion: Used to convert a type with a large value range to a type with a small value range. However, if the value to be converted is greater than or less than the value range, an error is returned, if this conversion method is used, the string cannot be converted to the int type, and an error is returned.

2. Int32.Parse () Conversion: It is used during string-to-int conversion in numeric format, and an exception can be thrown to the wrong string numeric format.

3. Convert. ToInt32 () Conversion: Using this type of conversion, the provided string must be a valid expression of the value, and this number must not be an overflow number. Otherwise, an exception is thrown.


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.