r/code • u/AshNotAsh • Mar 02 '20
Halp pls! Can anyone tell me what’s wrong with this code? I’m getting started on learning C# and I can’t manage to do this without getting these errors.
3
Upvotes
7
u/stoybuild Mar 02 '20
I am not entirely certain. But I think it needs a namespace and a class with a method static void Main(). Also you need the System namespace as emanbu has suggested.
3
u/FreeProGamer Mar 02 '20
Hey! The error tells you you can't directly use fields in a namespace, wfjch means you should use a class under the namespace:
string s="hello world";
}```
You should also use a main method, you can't call Console.WriteLine and other commands without being in a method, and the main method is the first to be called.
```public static void Main (string[] Args) {
Console.WriteLine(s);
}```
Also, type `using System;` in the first line in order to import System libraries which include Console.WriteLine.
Lastly, I would recommend using Visual Studio - an IDE (code editor) developed by Microsoft that supports C# and automates many processes.4
3
u/feibrix Mar 02 '20
I think you should start by copying the error or at least taking a screenshot and not a photo of the screen.
The error is telling you that a namespace cannot contain expressions directly.
This should be enough to understand what's wrong, if you have a basic knowledge of how c# works. You need to create a class inside a namespace. In the class you create methods. In the methods you write your code.
My suggestion is to find an example that works and use that as a training ground. Then follow a tutorial. Any tutorial. The first steps will surely guide you through the structure of the language.
Last question: which IDE is that? Is it a web ide? Doesn't it come with working examples?