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);
}
[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);
}
相关文章推荐
- (C#基本语法)4.运算符与表达式
- [叩响C#之门]第3章 运算符和表达式 3.1算术运算符(附:由年月日计算星期的吉姆拉尔森公式)
- C#学习之路,学习笔记 第三章 运算符和表达式(运算符、优先级、类型转换)
- C#系列四《C#运算符与表达式》
- 追着嘟嘟学c#系列入门第四篇-----c#的运算符和表达式【蓝鸥出品】
- [叩响C#之门]第3章 运算符和表达式 3.2 自增、自减运算符
- c#中如何将表达式中的数字和运算符分离
- C#编程自学之运算符和表达式
- 【第二篇日记】C# 的转义字符,结构定义、字符转换、类型检查、运算符以及正则表达式
- C#基础 结构、枚举、表达式、运算符
- 《C#精彩实例教程》小组阅读06 -- C#运算符与表达式
- (原创)c#学习笔记03--变量和表达式04--表达式01--数学运算符
- 从C#到Python —— 2 运算符、表达式和流程控制
- 快速撑握C#知识点之运算符与表达式
- C#基础知识——运算符与表达式
- C#学习之语句、表达式和运算符
- C#从入门到精通之表达式与运算符
- C#系列-C#运算符与表达式[5]
- C#基础(四)--运算符及表达式
- 用《叩响C#之门》复习C#基础知识 第三章 运算符和表达式