IS and as operators

Source: Internet
Author: User

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

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.