- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the purpose of ‘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
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(); } }
Advertisements