手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#中is与As运算符号的使用详解
C#中is与As运算符号的使用详解
摘要:如下所示:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem....

如下所示:

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class IsOrAsClass

{

class Animal

{

public void Eat()

{

Console.WriteLine("Eating...");

}

public override string ToString()

{

return "I am Eating";

}

}

//家禽类

class jia:Animal

{

}

//狗

class Dog : jia

{

}

//鸟

class bird

{

}

static void Main()

{

IsOrAsClass app=new IsOrAsClass();

//

Dog d=new Dog();

app.UseIsOpreate(d);

app.UseAsOpreate(d);

//

bird b = new bird();

app.UseAsOpreate(b);

}

//使用Is运算符

void UseIsOpreate(Animal a)

{

if (a is jia)

{

jia j = (jia)a;

j.Eat();

}

}

//使用AS运算符

void UseAsOpreate(object o)

{

jia j = o as jia;

if (j != null)

{

Console.WriteLine(j.ToString());

}

else

{

Console.WriteLine("{0} is not Animal", o.GetType().Name);

}

}

}

}

【C#中is与As运算符号的使用详解】相关文章:

c# 空合并运算符“??”的使用详解

string类的使用方法详解

解析C#中@符号的几种使用方法详解

C#基础:Equals()与运算符==的区别分析

C# 4.0 大数的运算--BigInteger的应用详解

解析C#中的装箱与拆箱的详解

C# 位运算符整理

浅谈Silverlight 跨线程的使用详解

基于动态修改App.Config与web.Config的使用详解

基于WebRequest.RegisterPrefix的使用详解

精品推荐
分类导航