Is 和 As 之惑
前言:
之前看过Anytao的《你必须知道的.NET》,其中有一章专门介绍Is 和 As 之惑的文章,当时感觉比较简单,就过去了。然而由上一篇博文《选取页面全部TextBox,并赋值的两种方法——从服务器端和JS实现》中再次用到了 Is 关键字(见下代码),然而却感觉印象模糊,所以在此复习之。
protected void Button1_Click(object sender, EventArgs e)
{
//选取form1中的全部控件,为简略,这里不能实现递归,只是实现单层选取
foreach (Control ct in this.FindControl ("form1").Controls )
{
if (ct is TextBox )
((TextBox)ct).Text = string.Empty;
}
}
{
//选取form1中的全部控件,为简略,这里不能实现递归,只是实现单层选取
foreach (Control ct in this.FindControl ("form1").Controls )
{
if (ct is TextBox )
((TextBox)ct).Text = string.Empty;
}
}
对于它们的用法,印象只停留在之前在项目《员工假勤管理系统中》,为了保存单个用户的信息,采用的Session 存储和 用As 提取的模式。
as 和 is 解析:
as 和is 都是在C#安全类型下的转换形式。其规则如下:
- is 用于转换时,首先作用为Boolean型判断,然后才能进行转换。这里的is ,同在C++和C#中经常提到的,在继承层次时的 “对象2 是 对象1”的说法相同;
- as 用于转换时,当可以有效转换时,通过一次转换即可。如果无法有效转换,则其为null 值;
注:
as 只能用于引用类型的转换。同时is 和 as 都不会抛异常(因为它们的特性)。见下例:
// Description: 演示类在继承后,所采用is 和as 的操作
// CopyRight: http://www.cnblogs.com/yangmingming
// Notes: 采用最为简单的类继承关系
namespace IsVSAsDemo
{
//Animal基类
public class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public virtual void ShowAnimal()
{
Console.WriteLine("I am an animal.");
}
}
//Animal的派生类
public class Bird : Animal
{
private string type;
public string Type
{
get { return type; }
set { type = value; }
}
public override void ShowAnimal()
{
Console.WriteLine("I am animal and {0}", Type);
}
}
//主程序
class Program
{
static void Main(string[] args)
{
Bird b = new Bird();
b.Name = "niao";
b.Type = "bird";
Animal animal = b;
// is 综合应用;
//应用is 1:
if (animal is Bird)
Console.WriteLine("This animal is Bird");
else
Console.WriteLine("This animal is not Bird");
//应用is 2:
if (b is Bird)
Console.WriteLine("bird is Bird");
else
Console.WriteLine("bird is not Bird");
//应用is 3:
if (b is Animal)
Console.WriteLine("bird is Animal");
else
Console.WriteLine("bird is not Animal");
//as 的综合应用
// as 的应用1:
Bird acceptBird = animal as Bird;
acceptBird.ShowAnimal();
// as 的应用2 :
Animal a = new Animal();
Bird acceptBird2 = a as Bird;
if (acceptBird2 == null)
Console.WriteLine("We cannot accept Bird By as operator!");//当无法有效转换时,仍为null;
}
}
}
// CopyRight: http://www.cnblogs.com/yangmingming
// Notes: 采用最为简单的类继承关系
namespace IsVSAsDemo
{
//Animal基类
public class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public virtual void ShowAnimal()
{
Console.WriteLine("I am an animal.");
}
}
//Animal的派生类
public class Bird : Animal
{
private string type;
public string Type
{
get { return type; }
set { type = value; }
}
public override void ShowAnimal()
{
Console.WriteLine("I am animal and {0}", Type);
}
}
//主程序
class Program
{
static void Main(string[] args)
{
Bird b = new Bird();
b.Name = "niao";
b.Type = "bird";
Animal animal = b;
// is 综合应用;
//应用is 1:
if (animal is Bird)
Console.WriteLine("This animal is Bird");
else
Console.WriteLine("This animal is not Bird");
//应用is 2:
if (b is Bird)
Console.WriteLine("bird is Bird");
else
Console.WriteLine("bird is not Bird");
//应用is 3:
if (b is Animal)
Console.WriteLine("bird is Animal");
else
Console.WriteLine("bird is not Animal");
//as 的综合应用
// as 的应用1:
Bird acceptBird = animal as Bird;
acceptBird.ShowAnimal();
// as 的应用2 :
Animal a = new Animal();
Bird acceptBird2 = a as Bird;
if (acceptBird2 == null)
Console.WriteLine("We cannot accept Bird By as operator!");//当无法有效转换时,仍为null;
}
}
}
调试结果为:
由调试结果可对is 和as 的转换方式及结果进行了代码验证。
综述之,由上对is 和 as 的相关分析,以期进一步厘清is 和 as 这两种较常用的转换运算符的了解和学习。在以后的应用中,有所知道。呵呵~