The role of the c#is operator

Source: Internet
Author: User

The meaning of the IS operator: checks if the object is not a given type, or whether it can be converted to a given type, and if so, this operator returns TRUE.

Syntax for the IS operator:<operand> is <type>

The result of this expression is as follows: If <type> is a class type, and <operand> is the type, or it inherits the type, or it can be marshaled to that type, the result returns TRUE.

If <type> is an interface type, and <operand> is the type, or if it is the type that implements the interface, the result returns TRUE.

Returns True if <type> is a value type, and <operand> is the type, or it can be disassembled into that type.

is instance:

1 namespaceChapter112 {3     classChecker4     {5          Public voidCheck (Objectparam1)6         {7             if(param1 isClassA)8Console.WriteLine ("Variable can converted to ClassA");9             ElseTenConsole.WriteLine ("Variable can ' t is converted to ClassA."); One  A             if(param1 isimyinterface) -Console.WriteLine ("Variable can converted to IMyInterface."); -             Else theConsole.WriteLine ("Variable can ' t converted to imyinterface."); -  -             if(param1 ismystruct) -Console.WriteLine ("Variable can converted to MyStruct."); +             Else -Console.WriteLine ("Variable can ' t is converted to MyStruct."); +         } A     } at     InterfaceIMyInterface -     { } -     classClassa:imyinterface -     { } -     classClassb:imyinterface -     { } in  -     classClassC to     { } +  -     classClassd:classa the     { } *  $     structMystruct:imyinterfacePanax Notoginseng     { } -     class Program the     { +         Static voidMain (string[] args) A         { theChecker check =NewChecker (); +ClassA Try1 =NewClassA (); -ClassB Try2 =NewClassB (); $ClassC Try3 =NewClassC (); $CLASSD Try4 =NewCLASSD (); -MyStruct Try5 =Newmystruct (); -             ObjectTry6 =Try5; theConsole.WriteLine ("analyzing ClassA type variable:"); - check. Check (try1);WuyiConsole.WriteLine ("analyzing ClassB type variable:"); the check. Check (Try2); -Console.WriteLine ("analyzing ClassC type variable:"); Wu check. Check (try3); -Console.WriteLine ("analyzing CLASSD type variable:"); About check. Check (TRY4); $Console.WriteLine ("analyzing mystruct type variable:"); - check. Check (try5); -Console.WriteLine ("analyzing boxed mystruct type variable:"); - check. Check (TRY6); A Console.readkey (); +         } the     } -}

The role of the c#is operator

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.