C# Programming/Keywords/is
From Wikibooks, open books for an open world
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.StreamReader reader = new StreamReader("readme.txt");
bool b = reader is System.IO.TextReader;
// b is now set to true, because StreamReader inherits TextReader
C# Keywords | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Special C# Identifiers | ||||||||||
|