首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >is和as关键字的区别

is和as关键字的区别
EN

Stack Overflow用户
提问于 2010-09-24 18:59:49
回答 8查看 94.9K关注 0票数 124

请告诉我C#中的isas关键字有什么区别

EN

Stack Overflow用户

发布于 2016-11-08 03:11:43

IS和AS都用于安全类型转换

IS Keyword-->检查给定对象的类型是否与新对象类型兼容。它从不抛出异常。这是一个布尔型type..returns,可以为true或false

代码语言:javascript
复制
`student stud = new student(){}
if(stud is student){} // It returns true // let say boys as derived class
if(stud is boys){}// It returns false since stud is not boys type
 //this returns true when,
student stud = new boys() // this return true for both if conditions.`

AS关键字:检查给定对象的类型是否与新对象类型兼容。如果给定对象与新对象兼容,则返回非null,否则返回null。这会抛出一个异常。

代码语言:javascript
复制
`student stud = new student(){}
 // let say boys as derived class
boys boy = stud as boys;//this returns null since we cant convert stud type from base class to derived class
student stud = new boys()
boys boy = stud as boys;// this returns not null since the obj is pointing to derived class`
票数 1
EN
查看全部 8 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3786361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档