共用方式為


編譯器錯誤 CS1626

更新:2007 年 11 月

錯誤訊息

無法在具有 catch 子句的 try 區塊主體中使用 yield 產生值

如果 try 區塊與 catch 子句相關聯,則 try 區塊中不可以有 yield 陳述式。若要避免這個錯誤,請將 yield 陳述式移出 catch 子句。

下列範例會產生 CS1626:

// CS1626.cs
using System.Collections;

class C : IEnumerable
{
   public IEnumerator GetEnumerator()
   {
      try
      {
         yield return this;  // CS1626
      }
      catch
      {
        
      }
   }
}

public class CMain
{
   public static void Main() { }
}