当前位置: 首页 > news >正文

3.C#知识点:is和as

IS和AS 都是用于类型转换的操作。

但是这两个有什么区别呢?

简单的来说 is 判断成立则返回True,反之返回false。as 成立则返回要转换的对象,不成立则返回Null。

下面掏一手代码来说明一下。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IsAndAsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            object child = new Child();
            bool b1 = (child is Father);
            bool b2 = (child is Mother);
            Console.WriteLine(b1);//返回true
            Console.WriteLine(b2);//返回false
            Console.ReadKey();
        }
    }
    public class Father
    {

    }
    public class Child:Father
    {

    }
    public class Mother
    {

    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IsAndAsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            object child = new Child();
            //bool b1 = (child is Father);
            //bool b2 = (child is Mother);
            //Console.WriteLine(b1);//返回true
            //Console.WriteLine(b2);//返回false
            //Console.ReadKey();
            Father f1 = child as Father;//可以得到转换成功,得到对象
            Mother m1 = child as Mother;//转换失败,m1的值为null
        }
    }
    public class Father
    {

    }
    public class Child:Father
    {

    }
    public class Mother
    {

    }
}

有一点我要强调一下就是子类可以转换父类,但是父类不能转换为子类,这就比如现在这个社会。你父母的东西是你的,但是你的东西还是你的一样。

 

            Father F2 = new Father();
            Child C1 = (Child)F2;

 

第二句代码转换的时候就会失败了。因为父类不能强转换为子类。Father不能转换为Child,但是我们如果用As进行转换的话为得到一个null的c1对象。

总结 

由他们返回值就可以简单的知道他们的用法。

is 主要用于类型推断,而不需要实际的转换。

as 主要用于真正的类型转换。

 

转载于:https://www.cnblogs.com/DingKing/p/8076341.html

相关文章:

  • springboot(十二):springboot如何测试打包部署
  • 两队列模拟一个栈,python实现
  • Lambda拉姆达表达式
  • 大型EMR电子病历源码三甲医院医疗信息管理系统软件网络版
  • 迭代器、 生成器、 面向过程编程
  • java-StringBuffer和String的相互转换
  • 数据库的SQL优化
  • DBMS_SCHEDULER修改资源调度总结
  • mysql5.6整表数据删除binlong恢复
  • 装饰器函数
  • 传奇物品叠加设置方法
  • 使用selenium抓取淘宝的商品信息
  • JAVA-初步认识-常用对象API(集合框架-概述)
  • 九:动态规划-最大的算式
  • ssl1104-USACO 2.1城堡(foodfill)【图论,广搜】
  • ES6系统学习----从Apollo Client看解构赋值
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • JAVA之继承和多态
  • Joomla 2.x, 3.x useful code cheatsheet
  • LeetCode29.两数相除 JavaScript
  • Markdown 语法简单说明
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • Phpstorm怎样批量删除空行?
  • Sublime Text 2/3 绑定Eclipse快捷键
  • vue 个人积累(使用工具,组件)
  • win10下安装mysql5.7
  • 从零开始学习部署
  • 关于List、List?、ListObject的区别
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 使用 QuickBI 搭建酷炫可视化分析
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • Java总结 - String - 这篇请使劲喷我
  • postgresql行列转换函数
  • Spring第一个helloWorld
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ​卜东波研究员:高观点下的少儿计算思维
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • #etcd#安装时出错
  • #include
  • #QT(串口助手-界面)
  • #每天一道面试题# 什么是MySQL的回表查询
  • ${factoryList }后面有空格不影响
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (1)虚拟机的安装与使用,linux系统安装
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (四)Android布局类型(线性布局LinearLayout)
  • (图)IntelliTrace Tools 跟踪云端程序
  • (学习日记)2024.01.09
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表