- C# 基础教程
- C# - 首页
- C# - 概述
- C# - 环境
- C# - 程序结构
- C# - 基本语法
- C# - 数据类型
- C# - 类型转换
- C# - 变量
- C# - 常量
- C# - 运算符
- C# - 决策制定
- C# - 循环
- C# - 封装
- C# - 方法
- C# - 可空类型
- C# - 数组
- C# - 字符串
- C# - 结构体
- C# - 枚举
- C# - 类
- C# - 继承
- C# - 多态
- C# - 运算符重载
- C# - 接口
- C# - 命名空间
- C# - 预处理器指令
- C# - 正则表达式
- C# - 异常处理
- C# - 文件 I/O
- C# 高级教程
- C# - 属性
- C# - 反射
- C# - 属性
- C# - 索引器
- C# - 委托
- C# - 事件
- C# - 集合
- C# - 泛型
- C# - 匿名方法
- C# - 不安全代码
- C# - 多线程
- C# 有用资源
- C# - 问题和解答
- C# - 快速指南
- C# - 有用资源
- C# - 讨论
C# - 杂项运算符
C# 支持一些其他重要的运算符,包括sizeof 和? :。
| 运算符 | 描述 | 示例 |
|---|---|---|
| sizeof() | 返回数据类型的尺寸。 | sizeof(int),返回 4。 |
| typeof() | 返回类的类型。 | typeof(StreamReader); |
| & | 返回变量的地址。 | &a; 返回变量的实际地址。 |
| * | 指向变量的指针。 | *a; 创建名为 'a' 的指向变量的指针。 |
| ? : | 条件表达式 | 如果条件为真 ? 则值为 X : 否则值为 Y |
| is | 确定对象是否为特定类型。 | If( Ford is Car) // 检查 Ford 是否是 Car 类的对象。 |
| as | 强制转换,如果转换失败则不抛出异常。 | Object obj = new StringReader("Hello"); StringReader r = obj as StringReader; |
示例
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
/* example of sizeof operator */
Console.WriteLine("The size of int is {0}", sizeof(int));
Console.WriteLine("The size of short is {0}", sizeof(short));
Console.WriteLine("The size of double is {0}", sizeof(double));
/* example of ternary operator */
int a, b;
a = 10;
b = (a == 1) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
b = (a == 10) ? 20 : 30;
Console.WriteLine("Value of b is {0}", b);
Console.ReadLine();
}
}
}
当以上代码被编译和执行时,会产生以下结果:
The size of int is 4 The size of short is 2 The size of double is 8 Value of b is 30 Value of b is 20
csharp_operators.htm
广告