6

I am writing the code right, but getting error - missing namespace or assembly reference.Is there something wrong with the code or I am missing something?

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

Error is : Error Cannot initialize type 'int' with a collection initializer because it does not implement 'System.Collections.IEnumerable

9
  • what 'using' statements do you have? Commented Aug 15, 2012 at 6:08
  • using System; using System.Collections.Generic; using System.Linq; using System.Text; Add these and your problem should solve Commented Aug 15, 2012 at 6:10
  • using System; should be enough. Commented Aug 15, 2012 at 6:10
  • 2
    @mahditahsildari: System.Collections.Generic? System.Linq? Those are not necessary. Commented Aug 15, 2012 at 6:11
  • 1
    -1 Please include the exact error message. Commented Aug 15, 2012 at 6:14

7 Answers 7

60

My namespace ended in Console (i.e. MyProject.Console) which messed up the calls to Console.Write. In this case, either write the fully qualified name System.Console.Write or change the namespace.

Sign up to request clarification or add additional context in comments.

So easy to do, so difficult to realize the problem!
25

i am writing the code right

Don't start with this assumption. Always start with the assumption that the compiler is correct, and your code is wrong.

You haven't shown any using directives. In this case all you need is

using System;

(Either at the very top of your code or within the namespace declaration.)

or change your WriteLine call to:

System.Console.WriteLine("Wow");

If that doesn't fix it (or if you've already got such a using directive but forgot to include it), then your project is probably somewhat broken - it's not like you're using any exotic types.

Supp Skeet, its like you live on Stackoverflow, Good Day man!
@Geek: Then the code you posted isn't the code you're compiling, and I don't see how we're meant to fix it...
@jon thanks i managed to solve the problem thanks for advice :)
3

Import the System namespace or just use System.Console.WriteLine("...");

using System;

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

Comments

2

I faced a similar problem. The namespace name I used ended with .Console, so there was a conflict with System.Console

using System;

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

I changed Test.Console to Test.ConsoleApp, and problem fixed for me

using System;

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

How embarrassing!
1

The minimum for your console app should have this

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

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

Thanks to ReSharper, my code would have only one of those directives .. (but only after using Console).
0

thanks to all for help i managed to solve the problem with the help of you guys :)

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

        }
    }
}

Comments

0

Using a namespace of Myproject.App also causes problems, in the same way as MyProject.Console (as per contactmatt's answer, above).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.