Delirium
Delirium

Reputation: 59

A namespace cannot directly contain members such as fields or methods, all functions are properly enclosed

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

This is my code. I tried to compile it in cmd, and that's the error it spat out. Anyone can explain whatever is going on here?

Side note, I created this file in VSCode, and it has no csproj file; it's simply a .cs file, nothing more. Maybe that could be the cause?

Upvotes: 0

Views: 43

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89091

The only thing wrong with that code is that you're missing a using statement. This

using System;

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

Compiles and runs through the .NET Framework compiler:

C:\test>c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc foo.cs

C:\test>foo.exe
hello world

Upvotes: 2

Related Questions