C# Keywords - is

记录一下在日常开发过程中遇到的一些C# 基础编程的知识!希望以后能用的着。知识是在平常的开发过程中去学到的。只有用到了,你才能深入的理解它,并用好它。

本资料来源于:MSND下面是一些相关的code 和 说明

C# is 关键字: 检查对象是否与给定类型相互兼容。

  C# Keywords - is 的使用

例如,下面的代码可以确定 obj对象是否为 MyObject 类型的一个实例,或者obj对象是否为从 MyObject 派生的一个类型:

这里有三种情况:

1、obj 对象就是指定类型的实例。 所以表达式是true

2、obj 对象是指定类型的基类,也就是MyObject : obj    表达式为 true

3、obj 对象与指定类型不兼容,那么表达式返回的是false.

if (obj is MyObject)
{
}

详细说明:

如果所提供的表达式非空,并且所提供的对象可以强制转换为所提供的类型而不会导致引发异常,则 is 表达式的计算结果将是 true。

如果已知表达式将始终是 true 或始终是 false,则 is 关键字将导致编译时警告,但是,通常在运行时才计算类型兼容性。

不能重载 is 运算符。

请注意,is 运算符只考虑引用转换、装箱转换和取消装箱转换。 不考虑其他转换,如用户定义的转换。

在 is 运算符的左侧不允许使用匿名方法。 lambda 表达式属于例外。

   示例:

class Class1 {}
class Class2 {}
class Class3 : Class2 { }

class IsTest
{
    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)
        {
            Console.WriteLine("o is Class2");
            b = (Class2)o;
            // Do something with "b."
        }

        else
        {
            Console.WriteLine("o is neither Class1 nor Class2.");
        }
    }
    static void Main()
    {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2();
        Class3 c3 = new Class3();
        Test(c1);
        Test(c2);
        Test(c3);  // 因为class3 继承class2 ,或这么理解: class2 是class3的基类。 在用is 关键字的时候。它会返回true.
        Test("a string");
    }
}
/*
Output:
o is Class1
o is Class2
o is Class2
o is neither Class1 nor Class2.
*/
时间: 2024-10-12 21:03:07

C# Keywords - is的相关文章

【HDU2222】Keywords Search

Problem DescriptionIn the modern time, Search engine came into the life of everybody like Google, Baidu, etc.Wiskey also wants to bring this feature to his image retrieval system.Every image have a long description, when users type some keywords to f

HDU 2222 Keywords Search AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67122    Accepted Submission(s): 22584 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 Keywords Search(瞎搞)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 50451    Accepted Submission(s): 16236 Problem Description In the modern time, Search engine came into the life of everybody lik

Keywords Search

Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords t

hdu 2222 Keywords Search(AC自动机入门)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42138    Accepted Submission(s): 13289 Problem Description In the modern time, Search engine came into the life of everybody like

Java文法(6)—keywords

-------------------------------------------------------------------------------------------------------------------------- 说明: 50 character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8

hdu----(2222)Keywords Search(trie树)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35683    Accepted Submission(s): 11520 Problem Description In the modern time, Search engine came into the life of everybody like

Keywords Search (ac 自动机)

Keywords Search  Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users typ

HDU 2222 Keywords Search (AC自动机模板题)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik

python-标识符(Identifiers)和关键字(keywords)

标识符:Identifiers 标识符必须以字母(大小写均可)或者"_"开头,接下来可以重复0到多次(字母|数字|"_") 特点: 1.没有长度限制 2.区分大小写 用处: 用于作为变量,函数名,类名,方法名等 关键字:keywords 关键字其实就是python内部已经使用了的标识符,如果使用这些关键字,将会覆盖python内置的功能,可能会导致无法预知的错误. 包括: and del from not while as elif global or with a