Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Concrete Application of is operator in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the specific application of the is operator in C#". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the specific application of the is operator in C#".

Is Application of C # operator

The is operator of the C # operator is used to check whether the runtime object type is compatible with the given type. The e in the expression "e is T" is an expression and T is a type. The return value is a Boolean value.

The is expression evaluates to a true value if the following two conditions are met:

Expression is not null.

Expression can be converted to type. That is, the conversion expression of the form (type) (expression) is completed without throwing an exception.

Application example of is for C # operator

/ / cs_keyword_is.cs / / The is operator using System; class Class1 {} class Class2 {} public class IsTest {public static void Test (object o) {Class1 a; Class2 b; if (o is Class1) {Console.WriteLine ("o is Class1"); a = (Class1) o / / do something with a} else if (o is Class2) {/ / C # operator's is application Console.WriteLine ("o is Class2"); b = (Class2) o; / / do something with b} else {Console.WriteLine ("o is neither Class1 nor Class2.");} public static void Main () {Class1 C1 = new Class1 (); Class2 c2 = new Class2 (); Test (C1); Test (c2) Test ("a string");}}

Output of the is application instance of the C # operator:

O is Class1 o is Class2 o is neither Class1 nor Class2. Thank you for your reading, the above is the content of "the specific application of the is operator in C#". After the study of this article, I believe you have a deeper understanding of the specific application of the is operator in C#, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report