您的位置:首页 > 编程语言 > C#

C#运算符与表达式

2012-05-24 16:53 309 查看
the operator and expression are similar to C++.However C# had added some new and useful operators ,for example :is,as,etc.

[b]is operator [/b]

the is operator is used for checking if the operation-type is equal or able to exchange to each other.the is operator is particular fitted for Polymorphism condition.There are two operator numbers and it's answer is bool type.referent example:

void function(object param)

{

if(param is ClassTest)

{do something;}

else if(param is not ClassTest)

{do some other things;}

}

[b]as operator[/b]

this operator can check if the operator is equal or able to change to each other.(as operator is done by is-operator).code reference as following:

Shap shp=new Shap();

vehicle veh=shp as vehicle;//return null,its type can not be transformed

Circle cir=new Circle();

shape shp=cir;

Circle cir2=shp as circle;

object[] objects=new object[2];

objects[]="Aisha";

object[1]=new Shap();

string str;

for(int i=0;i&<objects.Length;i++)

{

str=objects[i] as string;

if(str==null)

Console.WriteLINE("CAN NOT BE CONVERTED");

else

console.WriteLine(str);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: