The Is operator, also known as the type compatibility operator, plays an integral role in C# structures. Let's try to understand this operator.
C#'s Is operator checks whether a given object is compatible with another object and gives a result of true if it is compatible. Otherwise return false.
expression is obj
Expression is the object you want to check for compatibility. Expressions can contain variables, literals, and method calls. Obj is the type against which the expression is validated. This can include built-in types and user-defined types.
// The operation of the type compatibility operator is performed. Console.Writeline("Happy Holidays" is string); Console.Writeline(42 is string);
True False
Let us understand this output. We know that "Happy Holidays" is a string literal and 42 is an integer. When "Happy Holidays" is checked against the string data type, the result is true because it is compatible. When checked against the string, 42 yields false because it is incompatible.
Literal expressions consist of numbers, character sequences (strings), arrays, etc.
// The operation of the type compatibility operator is performed. Console.Writeline("Happy Holidays" is string);
TRUE
A variable expression will contain an object that acts as a container to hold a value or reference.
// an object is declared with string data type. object str= "Happy Holidays"; // The operation of the type compatibility operator is performed. Console.Writeline(str is string);
TRUE
The function call expression will make a function call on the left side of the is operator.
// A class declaration class class_dec{} // an object is declared. object str= Method_in_the_class(); // The operation of the type compatibility operator is performed. Console.Writeline(str is class_dec);
TRUE
In the above example, the function call statement is checked for type compatibility. As long as the function being called is declared in the type. It will turn out to be true. In this case the result will be wrong. class_dec is an empty class.
Predefined types in C# can be used on the right side of the is operator. It can be integer, character, floating point and boolean.
// an object is declared with numeric data type. object num= 42; // The operation of the type compatibility operator is performed. Console.Writeline(num is int);
TRUE
User-defined types can also be checked via the is operator. It consists of classes, enumerations, etc.
// A class declaration class class_dec{} // an instance of the class is declared. class_dec str= new class_dec(); // The operation of the type compatibility operator is performed. Console.Writeline(str is class_dec);
TRUE
In the above example, the is operator compares an object to a user-defined data type.
Note - The is operator can also be used with NULL. If the expression is not null, the output of this operator will always be false.
The range of user-defined types affects the output. The is operator should always be used within the scope of the declared type.
In this article, we focus on the is operator in C#. We analyzed the syntax and learned about the various instances where the is operator can be used. The use of the is operator is illustrated using various code snippets and examples.
The above is the detailed content of is operator in C#. For more information, please follow other related articles on the PHP Chinese website!