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

C#中使用as关键字将对象转换为指定类型

目录

一、定义

二、示例

三、生成


        使用as关键字可以将对象转换为指定类型,与is关键字不同,is关键字用于检查对象是否与给定类型兼容,如果兼容则返回true,如果不兼容则返回false。而as关键字会直接进行类型转换,如果转换成功将会返回转换后的对象,如果转换不成功则不会抛出异常而是返回NULL。

一、定义

        as关键字用于引用类型之间执行转换。

        as关键字与is关键字一样都不会抛出异常,但是相比之下as关键字要更方便一些。

二、示例

        首先创建一个字符串对象,然后将字符串对象隐式转换为object类型的对象Temp_obj,使用as关键字将Temp_obj转换为字符串对象,最后检查字符串对象是否为空。如果为空,则转换不成功;如果不为空,则转换成功并执行后面的语句块。

// 使用as关键字将对象转换为指定类型
namespace _026
{public partial class Form1 : Form{private GroupBox? groupBox1;private RadioButton? radioButton3;private RadioButton? radioButton2;private RadioButton? radioButton1;private Label? label1;private Button? button1;public Form1(){InitializeComponent();Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // label1// label1 = new Label{AutoSize = true,Location = new Point(29, 21),Name = "label1",Size = new Size(43, 17),TabIndex = 0,Text = "将FileStream对象转换为:"};// // radioButton1// radioButton1 = new RadioButton{AutoSize = true,Location = new Point(180, 17),Name = "radioButton1",Size = new Size(102, 21),TabIndex = 1,TabStop = true,Text = "转换为object类型",UseVisualStyleBackColor = true};// // radioButton2// radioButton2 = new RadioButton{AutoSize = true,Location = new Point(180, 41),Name = "radioButton2",Size = new Size(102, 21),TabIndex = 2,TabStop = true,Text = "转换为Stream类型",UseVisualStyleBackColor = true};// // radioButton3// radioButton3 = new RadioButton{AutoSize = true,Location = new Point(180, 65),Name = "radioButton3",Size = new Size(102, 21),TabIndex = 3,TabStop = true,Text = "转换为String类型",UseVisualStyleBackColor = true};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(0, 1),Name = "groupBox1",Size = new Size(354, 103),TabIndex = 0,TabStop = false,Text = "类型转换"};groupBox1.Controls.Add(radioButton3);groupBox1.Controls.Add(radioButton2);groupBox1.Controls.Add(radioButton1);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // button1// button1 = new Button{Location = new Point(137, 110),Name = "button1",Size = new Size(75, 23),TabIndex = 0,Text = "转换类型",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(354, 141);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";StartPosition = FormStartPosition.CenterScreen;Text = "as关键字转换类型";           groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}private void Button1_Click(object? sender, EventArgs e){if (radioButton1!.Checked)                //选择转换为object类型{using FileStream Temp_filestream =    //创建文件流对象new(@"d:\log.txt", FileMode.Create);if (Temp_filestream != null)          //判断转换是否成功{MessageBox.Show("转换为Object成功!", "提示!");}else{MessageBox.Show("转换为Object失败!", "提示!");}}if (radioButton2!.Checked)               //选择转换为stream类型{using FileStream Temp_filestream =   //创建文件流对象new(@"d:\log.txt", FileMode.Create);object Temp_obj = Temp_filestream;if (Temp_obj as Stream != null)      //判断转换是否成功{MessageBox.Show("转换为Stream成功!", "提示!");}else{MessageBox.Show("转换为Stream失败!", "提示!");}}if (radioButton3!.Checked)                //选择转换为string类型{using FileStream Temp_filestream =    //创建文件流对象new(@"d:\log.txt", FileMode.Create);object Temp_obj = Temp_filestream;if (Temp_obj as string != null)       //判断转换是否成功{MessageBox.Show("转换为String成功!", "提示!");}else{MessageBox.Show("转换为String失败!", "提示!");}}}}
}

三、生成

 

 

 

http://www.lryc.cn/news/273390.html

相关文章:

  • 【Spring实战】21 Spring Data REST 常用功能详细介绍
  • 05-微服务-RabbitMQ-概述
  • jmeter参数化的三种方式
  • java基础之Java8新特性-Lambda
  • 入门使用mybatis-plus
  • ubuntu安装和配置ssh教程
  • 每天刷两道题——第六天
  • 时间序列平稳性相关检验方法
  • <leetcode修炼>双指针训练-移动零
  • Python初探:从零开始的编程奇妙之旅
  • 算法与数据结构之链表<一>(Java)
  • 目标检测COCO数据集与评价体系mAP
  • 2024最全面且有知识深度的web3开发工具、web3学习项目资源平台
  • Golang - defer关键字 深入剖析
  • 如何在Spring Boot中使用@Scheduled写定时任务判断数据量是否过大,过大则进行分表操作,多张表使用临时视图查询
  • 使用jieba库进行中文分词和去除停用词
  • C语言之分支与循环【附6个练习】
  • 使用通用MCU实现无人机飞行任务的快速二次开发
  • 什么是Selinux
  • 计算机网络知识点
  • Qt 连接 Mysql
  • HarmonyOS4.0系统性深入开发14AbilityStage组件容器
  • 客服系统接入FastGPT
  • Hi5 2.0 虚拟手与追踪器(Tracker)的位置修正
  • 广播及代码实现
  • QT应用篇 三、QML自定义显示SpinBox的加减按键图片及显示值效果
  • 2022年全国职业院校技能大赛网络安全竞赛试题1-10-B模块总结
  • 20231228在Firefly的AIO-3399J开发板的Android11的Firefly的AIO-3399J开发板的DTS配置单前置摄像头ov13850
  • php-fpm运行一段时间,内存不足
  • 基于轻量级GhostNet模型开发构建生活场景下生活垃圾图像识别系统