November 19, 2017
The “is” operator
The “is operator” is used to check the type of a variable/object. The following example shows how to do it:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class Person { } class Program { static void Main(string[] args) { int i = 10; if(i is object) { Console.WriteLine("i is an object"); } float b = 10.5F; if(b is int) { Console.WriteLine(" b is int "); } var s = "abc"; if( s is double) { Console.WriteLine("s is string"); } Person p = new Person(); if( p is Person) { Console.WriteLine("p is Person"); } } } }