Advertisement
expression is type
//C# is Operator Example
using System;
class A
{
}
//Class B inheriting from class A
class B : A
{
public static void Main()
{
//Creating an object of class A
A a = new A();
//Creating an object of class B
B b = new B();
//The first expression involving the is operator
bool result = a is A;
Console.WriteLine("Is a an object of type A : " + result);
//The second expression involving the is operator
result = a is B;
Console.WriteLine("Is a an object of type B : " + result);
//The third xpression involving the is operator
result = b is A;
Console.WriteLine("Is b an object of type A : " + result);
//The fourth expression involving the is operator
result = b is A;
Console.WriteLine("Is b an object of type B : " + result);
}
}
Output
Is a an object of type A : True
Is a an object of type B : False
Is b an object of type A : True
Is b an object of type B : True
bool result = a is A;
The first is operator expression has returned true because an object of class A is tested against its own class type.bool result = a is B;
The second is operator expression has returned false because an object of class A is tested against its derived class type.bool result = b is A;
The third is operator expression has returned true because an object of class B is tested against its base class type.bool result = b is B;
The fourth is operator expression has returned true because an object of class B is tested against its own class type.
//C# The is Operator Example, testing against the Object class i.e. mother of all class in C#
using System;
class A
{
}
//Class B inheriting from class A
class B : A
{
public static void Main()
{
//Creating an object of class A
A a = new A();
//Creating an object of class B
B b = new B();
//The first expression involving the is operator
bool result = a is Object;
Console.WriteLine("Is a an object of type Object : " + result);
//The second expression involving the is operator
result = b is Object;
Console.WriteLine("Is b an object of type Object : " + result);
}
}
Is a an object of type Object : True
Is b an object of type Object : True
Advertisement
//C# The is operator testing against uninitialized objects
using System;
class A
{
public static void Main()
{
//Creating a reference variable object of class A i.e uninitialized object
A a;
//The first expression involving the is operator
bool result = a is A;
Console.WriteLine("Is a an object of type A : " + result);
}
}
A.cs(14,16): error CS0165: Use of unassigned local variable 'a'
//C# The is oprator example with null
using System;
class A
{
public static void Main()
{
//The second expression involving the is operator
bool result = null is A;
Console.WriteLine("Is null an object of type A : " + result);
//The third expression involving the is operator
result = null is Object;
Console.WriteLine("Is null an object of type Object : " + result);
}
}
Is null an object of type A : False
Is null an object of type Object : False
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement