Use is and as to check and convert compatible types

Source: Internet
Author: User

Previously, I used the forced conversion operation to check the type of an instance. It was written in this way when Pascal was just transferred to C ++, and the code was a bit clumsy.

 

(C # code)

Bool CheckIsType (object source)
{
Try
{
TagClass result = (TagClass) source;
Return true;
}
Catch
{
Return false;
}
}
Later, we found object. GetType (), but it is difficult to determine whether the instance type is compatible. Now I recommend the is and as operators.

Is checks whether the object is compatible with the given type.

The as operator is used for conversion between compatible reference types.

Why are you recommended:

The as operator is similar to the forced conversion operation. If the conversion fails, as returns null, and forced conversion causes an exception.

In some cases, we just want to judge the type, and there is no need to perform the conversion, so we can use is.

The following is an example of as (the code is referenced from MSDN ):

Using System;
Class Class1
{
}

Class Class2
{
}

Class MainClass
{
Static void Main ()
{
Object [] objArray = new object [6];
ObjArray [0] = new Class1 ();
ObjArray [1] = new Class2 ();
ObjArray [2] = "hello ";
Objar Ray [3] = 123;
Objar Ray [4] = 123.4;
ObjArray [5] = null;
For (int I = 0; I <objArray. Length; ++ I)
{
String s = objArray [I] as string;
Console. Write ("{0}:", I );
If (s! = Null)
{
Console. WriteLine ("'" + s + "'");
}
Else
{
Console. WriteLine ("not a string ");
}
}
}
}

The following is an example (the code is referenced from MSDN ):

Using System;
Class Class1
{
}

Class Class2
{
}

Class IsTest
{
Static void Test (object o)
{
Class1;
Class2 B;

If (o is Class1)
{
Console. WriteLine ("o is Class1 ");
A = (Class1) o;
// Do something with "."
}
Else if (o is Class2)
{
Console. WriteLine ("o is Class2 ");
B = (Class2) o;
// Do something with "B ."
}
Else
{
Console. WriteLine ("o is neither Class1 nor Class2 .");
}
}
Static void Main ()
{
Class1 c1 = new Class1 ();
Class2 c2 = new Class2 ();
Test (c1 );
Test (c2 );
Test ("a string ");
}
}

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.