目录
一、定义
二、示例
三、生成
在程序的开发过程中经常会使用类型转换,如果类型转换不成功则会出现异常,从抛出异常到捕获并处理异常,无形中增加了系统的开销,而且太过频繁地处理异常还会严重地影响系统的稳定性。is关键字可以有效地解决上面出现的问题。is关键字用于检查对象是否与给定类型兼容,如果兼容则返回true,如果不兼容则返回false。在进行类型转换前,可以先使用is关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。
一、定义
is关键字主要用于检查对象是否与指定类型兼容,如果兼容则返回true,如果不兼容则返回false。
二、示例
首先创建一个字符串对象,然后将字符串对象隐式转换为 object类型的对象,最后使用 is关键字判断 object类型的对象是否与 Steam类型兼容。由于 object类型的对象是从 sting对象隐式转换而来,所以 object类型的对象不可能与 Stream类型兼容,结果为 false,不会执行后面的显式转换。使用is关键字的好处是在进行显式转换前可以首先判断转换是否成功,从而杜绝了类型转换中出现的异常。
// 使用is关键字检查对象是否与给定类型兼容
namespace _025
{public partial class Form1 : Form{private GroupBox? groupBox1;private GroupBox? groupBox2;private RadioButton? radioButton2;private RadioButton? radioButton1;private RadioButton? radioButton4;private RadioButton? radioButton3;private Button? button1;public Form1(){InitializeComponent();Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // radioButton2// radioButton2 = new RadioButton{AutoSize = true,Location = new Point(42, 49),Name = "radioButton2",Size = new Size(102, 21),TabIndex = 1,TabStop = true,Text = "文件对象",UseVisualStyleBackColor = true,};// // radioButton1// radioButton1 = new RadioButton{AutoSize = true,Location = new Point(42, 22),Name = "radioButton1",Size = new Size(102, 21),TabIndex = 2,TabStop = true,Text = "字符串对象",UseVisualStyleBackColor = true,};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(0, 0),Name = "groupBox1",Size = new Size(150, 100),TabIndex = 0,TabStop = false,Text = "对象"};groupBox1.Controls.Add(radioButton2);groupBox1.Controls.Add(radioButton1);groupBox1.SuspendLayout();// // radioButton4// radioButton4 = new RadioButton{AutoSize = true,Location = new Point(42, 49),Name = "radioButton4",Size = new Size(102, 21),TabIndex = 1,TabStop = true,Text = "Fileinfo类型",UseVisualStyleBackColor = true,};// // radioButton3// radioButton3 = new RadioButton{AutoSize = true,Location = new Point(42, 22),Name = "radioButton3",Size = new Size(102, 21),TabIndex = 2,TabStop = true,Text = "string类型",UseVisualStyleBackColor = true,};// // groupBox2// groupBox2 = new GroupBox{Location = new Point(165, 0),Name = "groupBox2",Size = new Size(150, 100),TabIndex = 0,TabStop = false,Text = "类型"};groupBox2.Controls.Add(radioButton4);groupBox2.Controls.Add(radioButton3);groupBox2.SuspendLayout(); // // button1// button1 = new Button{Location = new Point(114, 116),Name = "button1",Size = new Size(75, 23),TabIndex = 1,Text = "查看是否兼容",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(314, 151);Controls.Add(button1);Controls.Add(groupBox2);Controls.Add(groupBox1);Name = "Form1";StartPosition = FormStartPosition.CenterScreen;Text = "is关键字查看类型兼容"; groupBox1.ResumeLayout(false);groupBox1.PerformLayout();groupBox2.ResumeLayout(false);groupBox2.PerformLayout();}/// <summary>/// 容器1里的2个单选按钮只能二选一,要么字符串对象要么文件对象,/// 容器2里的2个单选按钮只能二选一,要么字符串类型要么文件类型,/// 当文件对象与文件类型配对检查时,判断为类型兼容,/// 当字符串对象与字符串类型配对检查时,判断为类型兼容,/// 其他配对情况均判断为类型不兼容。/// </summary>private void Button1_Click(object? sender, EventArgs e){object Temp_obj = radioButton1!.Checked ? //正确的为变量添加引用"C# 编程词典" : new FileInfo(@"d:\");if (radioButton3!.Checked) //判断选择了哪一个类型{if (Temp_obj is string) //判断对象是否为字符串类型MessageBox.Show( //提示兼容信息"对象与指定类型兼容", "提示!");elseMessageBox.Show( //提示不兼容信息"对象与指定类型不兼容", "提示!");}else{if (Temp_obj is FileInfo) //判断对象是否为文件类型MessageBox.Show( //提示兼容信息"对象与指定类型兼容", "提示!");elseMessageBox.Show( //提示不兼容信息"对象与指定类型不兼容", "提示!");}} }
}
三、生成