AlanZ2223
AlanZ2223

Reputation: 144

Simple C# Error when compiling and running the code

I'm trying to learn C# by watching tutorials and when I try to rename my class it gives me this

So this first error is basically when I make a new console solution and then declare a class and give it a name and then try to write out to the console it gives me two errors.

![1ST:Error 1 Invalid token '(' in class, struct, or interface member declaration C:\Users\Alan\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 10 23 ConsoleApplication1

2ND : Error 2 The type name 'Writeline' does not exist in the type 'System.Console' C:\Users\Alan\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 10 13 ConsoleApplication1

I clicked an save all and saved it to a folder on my desktop where I plan to put all my C# files but I get those errors and when I start out a new document clean it and write the console commands and build it doesnt give me any errors.

Is it that I need a constructor or that I need to save my files in a specific order or what?. ]1

Upvotes: 2

Views: 5678

Answers (4)

Ryan Mauldin
Ryan Mauldin

Reputation: 21

Anthony Pegram’s answer is correct. Adding the directive using System; at the top of any relative c# files, allows the compiler to omit the need to have the System. portion of the method namespace in front of System.Console.WriteLine().

However, if the console solution is named TestSolution and the console application project has the namespace TestSolution.Console; the System. declaration in front of System.Console.WriteLine() calls will still be needed.

This is due to ambiguity in the compiler as to the Console portion ending in the namespace. A console project named TestSolution.TestConsole, will not run into the namespace collision, unless the name of the class being written is Console.

Watch for potential naming collisions for namespaces, when building and naming libraries for .NET applications.

Upvotes: 2

Anthony Pegram
Anthony Pegram

Reputation: 126952

It looks like you have a call to System.Console.WriteLine outside of the body of a method. The compiler thinks you are trying to reference a type, which does not exist, and it is also telling you that ( is not legal at that particular point in the code file.

You likely have a class definition that might look something like this

class Program
{
     System.Console.WriteLine("hello world");
}

What you need is a class definition that looks more like

class Program
{
     public static void Main(string[] args)
     {
          System.Console.WriteLine("hello world");
     }
}

Upvotes: 6

nkvu
nkvu

Reputation: 5851

It seems that you've blown away the Main() method. Also, you shouldn't (can't, I guess) just have a method call floating in a class - it has to be within a method or constructor.

Try replacing the contents within namespace ConsoleApplication1 with the below get you started:

class Program
{
    static void Main(string[] args)
    {
        Person alan = new Person();
        alan.PersonName = "Alan";

        Console.WriteLine("Hi " + alan.PersonName);
        Console.ReadLine();
    }
}

class Person
{
    public string PersonName { get; set; }
}

PS. You probably should have Person in a separate file (e.g. Person.cs) but for getting started this should do....not the ideal end state, though.

Upvotes: 0

Alastair Pitts
Alastair Pitts

Reputation: 19601

You have a couple of issues.

Firstly, you are trying to call Console.WriteLine() in an invalid part of the file. You need to declare a Method or Constructor and place this call inside that.

Constructor example:

public class Person
{
    public Person()
    {
        Console.WriteLine("Hello");
    }
}

Method example:

public class Person
{
    public void SayHello()
    {
        Console.WriteLine("Hello");
    }
}

Your other issues is that you have the capitalization of 'WriteLine' incorrect.

The code should read:

Console.WriteLine("Test");

http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx

Upvotes: 0

Related Questions