错误:缺少命名空间或程序集引用。

5

我正在编写代码,但出现了错误-缺少名称空间或程序集引用。是我的代码有问题还是我漏掉了些什么?

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);
        }
    }
}

错误是:错误 无法使用集合初始化程序初始化类型'int',因为它没有实现'System.Collections.IEnumerable'。

你有哪些 'using' 语句? - elyashiv
使用 System; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 添加这些代码,你的问题就会解决。 - Mahdi Tahsildari
使用 System; 应该就足够了。 - elyashiv
2
@mahditahsildari:System.Collections.GenericSystem.Linq?这些不是必需的。 - Daniel Hilgarth
1
请包含精确的错误信息。 - user166390
显示剩余4条评论
7个回答

59

我的命名空间以 Console 结尾 (即 MyProject.Console),这混淆了对 Console.Write 的调用。在这种情况下,要么写全限定名 System.Console.Write,要么更改命名空间。


9
做起来很容易,但意识到问题却很困难! - tbone
2
谢谢,这正是我的问题! - Peter Tirrell

24

我正在编写代码

不要从这个前提开始。始终假设编译器是正确的,而你的代码是错误的。

你没有展示任何using指令。在这种情况下,你只需要

using System;
(Either at the very top of your code or within the namespace declaration.)
或者将您的WriteLine调用更改为:

or change your WriteLine call to:

System.Console.WriteLine("Wow");

如果那个方法不能解决问题(或者如果你已经有了这样的using指令,但是忘记包含它),那么你的项目可能有些损坏 - 这不像你使用任何奇异的类型。


嗨,Supp Skeet,你就像生活在 Stackoverflow 上一样,祝你有美好的一天! - JohnnBlade
@Geek:那么你发布的代码不是你正在编译的代码,我不知道我们该如何修复它... - Jon Skeet
@jon 谢谢,我成功解决了问题,感谢您的建议 :) - Geek

3

导入System命名空间,或者直接使用System.Console.WriteLine("...");

using System;

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

2

我曾经遇到过类似的问题。我的命名空间以.Console结尾,因此与System.Console发生了冲突。

using System;

namespace Test.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

我把 Test.Console 改成了 Test.ConsoleApp,问题就解决了。

using System;

namespace Test.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

1
多尴尬啊! - jasonscript

1

你的控制台应用程序至少应该具备以下要求

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

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

1
由于ReSharper的帮助,我的代码只需要一个指令就可以了...(但前提是使用了Console)。 - user166390

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

        }
    }
}

0

使用命名空间Myproject.App也会导致问题,就像MyProject.Console一样(如上面contactmatt的答案所述)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接