2012-08-15 120 views
0

我正在編寫代碼,但出現錯誤 - 缺少名稱空間或程序集引用。是否存在代碼錯誤或者我錯過了某些內容?錯誤缺少名稱空間或程序集引用

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication5 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int i = 0; 
      int sum = 0; 
      int[] arr = new int[] { 1, 2 }; 

      do 
      { 
       { 
        sum += arr[1]; 
        Console.WriteLine("Wow"); 
        i++; 
       } 
      } 
      while (i < 3); 
     } 
    } 
} 

錯誤是:錯誤不能用集合初始化初始化類型「詮釋」,因爲它沒有實現「System.Collections.IEnumerable

+0

你有什麼「使用」聲明? – elyashiv 2012-08-15 06:08:30

+0

使用系統; using System.Collections.Generic;使用System.Linq的 ; using System.Text; 添加這些和您的問題應該解決使用系統 – 2012-08-15 06:10:00

+0

;應該夠了。 – elyashiv 2012-08-15 06:10:34

回答

13

我寫的代碼正確

不要用這個假設開始。總是從假定編譯器是正確的,並且你的代碼是錯誤的開始。

您還沒有顯示任何using指令。在這種情況下,所有你需要的是

using System; 

(無論是在你的代碼或namespace聲明中的最頂端。)

或更改WriteLine呼籲:

System.Console.WriteLine("Wow"); 

如果不是修復它(或者如果你已經有這樣一個using指令,但忘了包括它),那麼你的項目可能有些破碎 - 它不像你使用Ÿ異國情調的類型。

+0

增刊飛碟雙向,它就像你住#2,好男人節! – JohnnBlade 2012-08-15 06:13:08

+0

我使用所有指令 – Geek 2012-08-15 06:39:29

+0

@Geek:那你發佈的代碼是不是你編譯代碼,我不知道我們是如何打算解決它...... – 2012-08-15 07:16:19

3

導入System命名空間或只是使用System.Console.WriteLine("...");

using System; 

namespace TestNs 
{ 
    public class Test 
    { 
     static void Main() 
     { 
     Console.WriteLine("Hello World"); 
     } 
    } 
} 
+0

感謝真正幫助 – Geek 2012-08-15 07:16:55

1

您的控制檯應用程序的最小數量應該是

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace TestConsole 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     } 
    } 
} 
+1

由於ReSharper的代碼,我的代碼將只有那些指令之一。(但使用'Console'後只)。 – 2012-08-15 06:19:40

+0

感謝您的幫助:) – Geek 2012-08-15 07:20:50

0

感謝所有的幫助我管理與你們的幫助:)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication5 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      int i = 0; //initialize integer i=0 
      int sum = 0; // initialize integer sum = 0; 
      int[] arr = new int[]{1, 2, 3, 4}; // array containing 4 integers elements 
      do 
      { 

       { 
        sum+=arr[i]; //sum each integer in array and store it in var sum 

        i++;  //increment i for each element of array 
        Console.WriteLine(sum); //output the var sum conatining values after each increment 
       } 

      } 
      while(i<=3); //check condition for number of elements in array 

     } 
    } 
} 
11

My命名空間中Console結束(即MyProject.Console),它搞砸調用Console.Write來解決這個問題。在這種情況下,請寫入完全限定名稱System.Console.Write或更改名稱空間。

+3

這麼容易做,很難實現這個問題! – tbone 2015-06-03 17:45:37

相關問題