更新:2007 年 11 月
错误消息
as 运算符必须与引用类型或可为 null 的类型一起使用(“int”是不可为 null 的值类型)。
向 as 运算符传递了值类型。由于 as 可以返回 null,因此只能向它传递引用类型或可以为 null 的类型有关可为 null 的类型的更多信息,请参见可空类型(C# 编程指南)。
下面的示例生成 CS0077:
// CS0077.cs
using System;
class C
{
}
struct S
{
}
class M
{
public static void Main()
{
object o1, o2;
C c;
S s;
o1 = new C();
o2 = new S();
s = o2 as S; // CS0077, S is not a reference type.
// try the following line instead
// c = o1 as C;
}
}