-1

The following is the "Hello World" program that I tried to get it compiled with, the Microsoft .Net compiler, by the command "cs".

Using System

public class csharp1
{
    private static void main()
    {
         console.writeline("yaaannn");
    }
}

But I got the error:

CS0116: A namespace does not directly contain members such as fields or methods

that too at (1,1)

I checked out for this error:

Error "A namespace does not directly contain members such as fields or methods"

In the above question the compilation was being done through "Visual studio ". So , I am not sure if the things about "app.config" file hold good here when the compilation is being done through inbuilt compiler and from command prompt.

Then I checked the following question:

"A namespace cannot directly contain members such as fields or methods" in Net.Reflector

But here as we can see there was a variable which was not enclosed inside the braces of a class, however in this code there was no variable which was out of the confinements of a class.

So, I am unable to know what is causing this problem.

I thought I am missing a semicolon after "Using system " but that too threw the same error.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Put your class "class csharp1" in a namespace. Something like using System; namespace NM { public class CSharp{}} – Praneet Nadkar Jul 24 '18 at 05:37
  • @PraneetNadkar: Namespaces aren't required, and adding one wouldn't help here. It's the `using` directive that's initially broken - and then the capitalization of `console.writeline`. – Jon Skeet Jul 24 '18 at 05:48
  • I would guess that you let this code get near something with an automatic spellchecker, such as an email program or word processor. Try not to let that happen again, and if it does, pay attention to capitalization changes. – Damien_The_Unbeliever Jul 24 '18 at 05:54

3 Answers3

1

No capital letter in using, ; after System, and Main with capital M, add namespace:

using System;
namespace Test
{
    class csharp1
    {
       static void Main()
       {
           console.writeline("yaaannn");
       }
    }
}
Backs
  • 24,430
  • 5
  • 58
  • 85
1

The compiler believes that this:

Using System 

... is trying to introduce a new member, but that's not in the context of a type, which is why you're getting that slightly confusing error message. C# is case-sensitive (both for keywords and for method names etc). A using directive needs to be using rather than Using, and it needs a semi-colon at the end:

using System;

Your code still won't compile, as:

  • Your main method should be Main to be an entry point. It's valid (but unconventional) to have a method called main, but then your program won't have an entry point.
  • console.writeline should be Console.WriteLine

So a minimal change to make it compile would be:

using System;

class csharp1
{
    static void Main()
    {
        Console.WriteLine("yaaannn");
    }
}

I'd also advise you to follow .NET naming conventions, which would name your class Csharp1 (or more conventionally Program for the class containing an entry point). For anything non-trivial, you'd also want to put your class in a namespace, although you don't have to.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

C# is case-senstive, so:

Using System

public class csharp1
{
    private static void main()
    {
         console.writeline("yaaannn");
    }
}

Should be:

using System;

public class csharp1
{
    public static void Main()
    {
         Console.WriteLine("yaaannn");
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42