C# Keywords - as

记录一下在日常开发过程中遇到的一些C# 基础编程的知识!

希望以后能用的着。知识是在平常的开发过程中去学到的。只有用到了,你才能深入的理解它,并用好它。

本资料来源于:MSND

下面是一些相关的code  和 说明。

As 关键字 (属于运算符关键字)

可以使用  as 运算符执行转换的某些类型 在兼容之间的引用类型 或可以为 null的类型。 这段话不好理解,说白了就是强制类型转换不会throw exception。

class Base

{

public override string ToString()

{

return "Base";

}

}

class Derived : Base

{

}

class Program

{

static void Main()

{

Derived d = new Derived();

Base b = d as Base;  // 引用类型的强制类型转换

if (b != null)

{

Console.WriteLine(b.ToString());

}

}

 重点注意:

As 运算符类似于强制类型转换操作,但是唯一不同的是,如果转换是不可能的,as会返回 null 而不引发异常。

expression as type    这两给表达式的效果是一样的。

Expression is type ? (type)expression : (type)null

注意的是: as 运算符执行只引用转换、nullable 转换和装箱转换。 as 运算符不能执行其他转换,如用户定义的转换,应是通过使用转换的表达式。

class ClassA { }
class ClassB { }

class Program

{

static void Main()
{
object[] obj = new object[6];
obj[0] = new ClassA();
obj[1] = new ClassB();
obj[2] = "Hello";
obj[3] = 100;
obj[4] = 365.23;
obj[5] = null;

foreach (var val in obj)
{
string str = val as string; // 如果此刻类型转换失败,不会抛异常,而是赋空引用 null
if (str != null)
{
         Console.WriteLine("string: " + str);
}
else
{
       Console.WriteLine(val + " not string type");
}
}
           Console.ReadLine();

}
}

时间: 2025-01-07 20:58:27

C# Keywords - as的相关文章

【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