Reputation: 233
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
Upvotes: 6
Views: 23379
Reputation: 5442
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!");
}
}
}
Upvotes: 2
Reputation: 101
Using a namespace of Myproject.App
also causes problems, in the same way as MyProject.Console
(as per contactmatt's answer, above).
Upvotes: 0
Reputation: 18600
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.
Upvotes: 60
Reputation: 233
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
}
}
}
Upvotes: 0
Reputation: 1499790
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.
Upvotes: 25
Reputation: 4327
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)
{
}
}
}
Upvotes: 1
Reputation: 94625
Import the System
namespace or just use System.Console.WriteLine("...");
using System;
namespace TestNs
{
public class Test
{
static void Main()
{
Console.WriteLine("Hello World");
}
}
}
Upvotes: 3