IS and as operators:
is to determine if it is a type that returns TRUE or False
o as Ren; if the conversion succeeds, no problem; As is used to convert
If the conversion is not successful, the error is not reported, but a null value is returned
Cases
Instantiate a collection and put it in a method of a class that you write:
This is the wrong way of writing, just here to give an example of the purpose and usage of IS and as (borrowing the project code to do the interface, the main part is traversing the collection below)
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceInterface {classProgram {Static voidMain (string[] args) {ArrayList arr=NewArrayList ();//Instantiate a collection//instantiate the method that Ren can use to all classesren sc =NewRen (); Random ran=NewRandom (); Arr. ADD (SC);//put the method that you wrote in the class into the collectionarr. ADD (RAN); foreach(ObjectOinchArr//Print this collection because you do not know the type of data in Ren and need to convert { if(O isren//Judging if O is the type of Ren, here is the IS, judging is not, return TRUE or False{ren scc= O asren//Converts o to the Ren type, where the as conversion is used, and if the conversion fails, returns a null, null value}//prevents a program from crashing and can cause a program to crash if it is easily turned on by another type} console.readline (); } }}
IS and as operators