常見C#編譯器錯誤

來源:互聯網
上載者:User

1、編譯器錯誤 CS1527

更新:2007 年 11 月

錯誤訊息
命名空間中定義的元素無法顯式聲明為 private、protected 或 protected internal

命名空間中的型別宣告可以具有 public 或 internal 訪問。如果沒有指定可訪問性,則預設是 internal。

下面的樣本產生 CS1527:

複製代碼
// CS1527.cs
namespace Sample
{
   private class C1 {};             // CS1527
   protected class C2 {};           // CS1527
   protected internal class C3 {};  // CS1527
}

2、編譯器錯誤 CS1597

更新:2007 年 11 月

錯誤訊息
方法或訪問器塊後面的分號無效

不需要(或不允許)使用分號結束方法或訪問器塊。

下面的樣本產生 CS1597:

Copy Code
// CS1597.cs
class TestClass
{
   public static void Main()
   {
   };   // CS1597, remove semicolon
}

----------------------------------------
但是class和namespace可以使用分號結束
如:

class TestClass
{
   public static void Main()
   {
   } 
};

 

3、編譯器錯誤 CS0542

更新:2007 年 11 月

錯誤訊息
“user-defined type”:成員名稱不能與它們的封閉類型相同

某一名稱在同一構造中多次使用。造成此錯誤的原因可能是因疏忽而將傳回型別放在了建構函式中。(也就是說建構函式不能有傳回型別)

下面的樣本產生 CS0542:

Copy Code
// CS0542.cs
class F
{
   // Remove void from F() to resolve the problem.
   void F()   // CS0542, same name as the class
   {
   }
}

class MyClass
{
   public static void Main()
   {
   }
}如果您的類被命名為“Item”,並且有一個聲明為 this 的索引器,您可能會遇到此錯誤。在發出的代碼中為預設索引器給出的名稱是

“Item”,從而導致衝突。

Copy Code
// CS0542b.cs
class Item
{
   public int this[int i]  // CS0542
   {
      get
      {
         return 0;
      }
   }
}

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

 

 

待續……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.