Home  >  Article  >  Backend Development  >  is operator in C#

is operator in C#

PHPz
PHPzforward
2023-09-13 23:01:021016browse

C# 中的 is 运算符

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.

grammar

expression is obj

Example

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);

Output

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.

expression

Literal expression

Literal expressions consist of numbers, character sequences (strings), arrays, etc.

Example

// The operation of the type compatibility operator is performed.
Console.Writeline("Happy Holidays" is string);

Output

TRUE

Variable expression

A variable expression will contain an object that acts as a container to hold a value or reference.

Example

// 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);

Output

TRUE

Function call expression

The function call expression will make a function call on the left side of the is operator.

Example

// 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);

Output

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.

type

Built-in types

Predefined types in C# can be used on the right side of the is operator. It can be integer, character, floating point and boolean.

Example

// 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);

Output

TRUE

User-defined types

User-defined types can also be checked via the is operator. It consists of classes, enumerations, etc.

Example

// 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);

Output

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 conclusion

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete