C#运算符与表达式

the operator and expression are similar to C++.However C# had added some new and useful operators ,for example :is,as,etc.

is operator 

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;}

as operator

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);

}

posted on 2012-05-24 16:53  .ne菜鸟  阅读(171)  评论(0编辑  收藏  举报

导航