
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to use ‘is’ operator in C#?
The "is" operator in C# checks whether the run-time type of an object is compatible with a given type or not.
The following is the syntax −
expr is type
Here, expr is the expression and type is the name of the type
The following is an example showing the usage of is operator in C# −
Example
using System; class One { } class Two { } public class Demo { public static void Test(object obj) { One x; Two y; if (obj is One) { Console.WriteLine("Class One"); x = (One)obj; } else if (obj is Two) { Console.WriteLine("Class Two"); y = (Two)obj; } else { Console.WriteLine("None of the classes!"); } } public static void Main() { One o1 = new One(); Two t1 = new Two(); Test(o1); Test(t1); Test("str"); Console.ReadKey(); } }
Output
Class One Class Two None of the classes!
- Related Articles
- How to use ‘as’ operator in C#?
- How to use Operator Overloading in C#?
- How to use Null Coalescing Operator (??) in C#?
- How to use an assignment operator in C#?
- How to use the ?: conditional operator in C#?
- How do I use the conditional operator in C/C++?
- How to use delete Operator in TypeScript?
- How to use typeof operator in TypeScript?
- What is the use of sizeof Operator in C#?
- How to use the "in" operator in JavaScript?
- How to use the instanceof operator in Java?
- How to use *= operator in jQuery attribute selector?
- How to use the slicing operator in Python?
- What is the use of scope resolution operator in C++?
- Why do we use comma operator in C#?

Advertisements