is Operator

The is operator checks if the run-time type of an expression result is compatible with a given type.
The is operator doesn't consider user-defined conversions.
The Is Operator only returns a boolean value
It can be useful when you want to determine the type but not necessarily cast
Beginning with C# 7.0, the is operator also tests an expression result against a pattern.


Checks if an object is compatible with a given type and always returns a boolean value
An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
You can use the Is operator to test whether a cast can be made successfully
The is operator cannot be overloaded.
If you know the specific base class, then just use the is keyword:
This is a cast parameter ?


string myobject = ""; 

if (myobject is string)
{
}
if (myobject is int)
{
}
if (myobject is class1)
{
}

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.
You can use the Is and As operators to test whether a cast can be made successfully.

class Dog : Animal {} 

void UseIsOperator(Animal a)
if (a is Dog)
{
}

Pattern Matching




VB.Net

The equivalent in VB.Net is the Is Operator.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext