- 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 use of "is" keyword in C#?
The "is" keyword is used to check if an object can be casted to a specific type. The return type of the operation is Boolean.
Example
using System; namespace DemoApplication{ class Program{ static void Main(){ Employee emp = new PermanentEmployee{ ID = 1, Name = "Martin" }; // Returns true as the derived type can be converted to base type. if (emp is Employee){ Console.WriteLine(emp.Name + " is Employee"); } else{ Console.WriteLine(emp.Name + " is not Employee"); } //Returns true, as the actual object is of type PermanentEmployee. if (emp is PermanentEmployee){ Console.WriteLine(emp.Name + " is PermanentEmployee"); } else{ Console.WriteLine(emp.Name + " is not PermanentEmployee"); } //Returns false, as PermanentEmployee object cannot be converted to //ContractEmployee. if (emp is ContractEmployee){ Console.WriteLine(emp.Name + " is ContractEmployee"); } else{ Console.WriteLine(emp.Name + " is not ContractEmployee"); } } } class Employee{ public int ID { get; set; } public string Name { get; set; } } class PermanentEmployee : Employee{ public int AnnualSalary { get; set; } } class ContractEmployee : Employee{ public int HourlySalary { get; set; } } }
Advertisements