I am using visual studio 2010.
It seems that out of nowhere, I can't use Console.WriteLine()
For example now I do:
- File -> New project
- Visual C# -> Console Application
Inside the Main method. Everything works fine. This works as expected:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Wor开发者_高级运维ks!");
Console.ReadLine();
}
}
}
Now when I add a new class, by clicking Test -> Add -> Class
Inside this new class I can't call Console.Writeline()
or anything rather.
Visual studio is already not suggesting it, but I ran it just in case and so that I could share the error and it gave me:
Error 1 El token '(' no es válido en una clase, un struct o una declaración de miembro de interfaz c:\users\trufa\documents\visual studio 2010\Projects\Test\Test\Class1.cs 10 26 Test
The translation would be:
The token "(" is not valid in a class, a struct, or interface member declaration...
Any ideas as to why this is happening?
It really seamed to happen out of nowhere.
I didn't think the question was for superuser but let me know if you think otherwise.
EDIT, Full code as is:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Works!");
Console.ReadLine();
}
}
}
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Class1
{
Console.WriteLine("Works!");//this one gives me the error
}
}
Just in case:
Have you typed the code directly into the class rather than placing in a method e.g?
public class Class1
{
Console.WriteLine();
}
Gives me the error
Error 32 Invalid token '(' in class, struct, or interface member declaration ...\Class1.cs 10 21 ConsoleTester
EDIT
Place the call inside a method, then it will compile. You can't place calls like that directly in the class itself.
public class Class1
{
public void Test()
{
Console.WriteLine();
}
}
精彩评论