is operator in C#

PHPz
Release: 2023-09-13 23:01:02
forward
982 people have browsed it

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
Copy after login

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);
Copy after login

Output

True
False
Copy after login

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);
Copy after login

Output

TRUE
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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);
Copy after login

Output

TRUE
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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);
Copy after login

Output

TRUE
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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);
Copy after login

Output

TRUE
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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);
Copy after login

Output

TRUE
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!