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