C# 程式設計/關鍵字/is
外觀
< C Sharp 程式設計 | 關鍵字
關鍵字is 將物件與型別進行比較,如果它們相同或屬於相同的“型別”(物件繼承該型別),則返回true。因此,該關鍵字用於檢查型別相容性,通常在強制轉換(轉換)源型別到目標型別之前,以確保不會丟擲型別轉換異常。在null 變數上使用is 始終返回false。
此程式碼片段展示了示例用法
System.IO.StreamReader reader = new StreamReader("readme.txt");
bool b = reader is System.IO.TextReader;
// b is now set to true, because StreamReader inherits TextReader
| C# 關鍵字 | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| C# 特殊識別符號(上下文關鍵字) | |||||||||||||||
| |||||||||||||||
| 上下文關鍵字(用於查詢) | |||||||||||||||
|