C# Programming/Keywords/is
Appearance
From Wikibooks, open books for an open world
The latest reviewed version was checked on 16 April 2020. There are template/file changes awaiting review.
C# Programming
Cover | Introduction | Basics | Classes | Advanced Topics | The .NET Framework | Index
Cover | Introduction | Basics | Classes | Advanced Topics | The .NET Framework | Index
The is
keyword compares an object to a type, and if they're the same or of the same "kind" (the object inherits the type), returns true . The keyword is therefore used to check for type compatibility, usually before casting (converting) a source type to a destination type in order to ensure that won't cause a type-cast exception to be thrown. Using is
on a null variable always returns false .
This code snippet shows a sample usage:
System.IO.StreamReaderreader=newStreamReader("readme.txt"); boolb=readerisSystem.IO.TextReader; // b is now set to true, because StreamReader inherits TextReader
C# Keywords |
---|
Special C# Identifiers (Contextual Keywords) |
Contextual Keywords (Used in Queries) |