共用方式為


編譯器錯誤 CS0077

更新:2007 年 11 月

錯誤訊息

as 運算子必須與參考型別或可為 Null 的型別 (Nullable Type) ('int' 就是不可為 null 的實值型別) 搭配使用。

as 運算子被傳遞了一個實值型別。由於 as 可以傳回 null,因此只能對它傳遞參考型別或可為 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;
   }
}