共用方式為


編譯器錯誤 CS0070

更新:2007 年 11 月

錯誤訊息

事件 'event' 只能出現在 += 或 -= 的左邊 (除非從型別 'type' 中使用)

在其所定義的類別外,事件只能加入或減少參考。如需詳細資訊,請參閱事件 (C# 程式設計手冊)

下列範例會產生 CS0070:

// CS0070.cs
using System;
public delegate void EventHandler();

public class A
{
   public event EventHandler Click;

   public static void OnClick()
   {
      EventHandler eh;
      A a = new A();
      eh = a.Click;
   }

   public static void Main()
   {
   }
}

public class B
{
   public int Foo ()
   {
      EventHandler eh = new EventHandler(A.OnClick);
      A a = new A();
      eh = a.Click;   // CS0070
      // try the following line instead
      // a.Click += eh;
      return 1;
   }
}