namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Animal ani = new Animal();
bool result = ani is Person;
object obj = "male";
Person per = obj as Person;
}
}
public class Person
{
public string Name
{
get;
set;
}
public string Gender
{
get;
set;
}
}
public class Animal
{
public int Age
{
get;
set;
}
}
}
bool result = ani is Person;
object obj = "male";
Person per = obj as Person;
这里用到了is和as,我想问的是is和as的区别具体是什么?还有什么别的地方会用到is和as么? 作者: 小人物 时间: 2013-4-25 19:01
as的作用是数据类型转换!is是相当于赋值!bool result = ani is Person;像这句代码!等号后面ani is Person说ani是人这个类,返回值是Bool,是就是true,不是就是false,在程序中不会报错!as的话就报错!后面Person per=boj as Person!是把Object类型的obj转换为Person!此处也可以这么写Person per=(Person)obj!希望能帮到你!作者: 黑马—严守卫 时间: 2013-4-27 10:55
is起的是判断的作用相当==,而as则作为数据转换使用的。