- 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
廣告