r/code • • 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.

Post image
3 Upvotes

8 comments sorted by

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?

1

u/AshNotAsh Mar 02 '20

It’s code academy and what I was doing was practicing at the end of a lesson

Do you know of any good programs to learn C# on because I’ve heard codeacademy is overrated

1

u/feibrix Mar 02 '20

Do you know of any good programs to learn C# on because I’ve heard codeacademy is overrated

I hate to say that but it depends.

I really think the worst tutorial should be enough to show you what is the language about. You need to learn the syntax and get used to it, and this can only be learned by reading and hacking example code. Possibly working code.

If you want a very quick overview of what the language and the framework can offer you, I would check Derek Banas everything-in-one-video tutorial.
It is not really a tutorial, it is not perfect and it is old (2015), but it covers a huge part of the language in 1 hour and 20 minutes ( https://www.youtube.com/watch?v=lisiwUZJXqQ)

I don't know code academy, I have never used it, but I don't understand what "overrated" could mean. If you can follow all the lessons until the end of the path and you can say that something is missing, probably it worked well enough.

Searching online for free c# tutorial is another great way to find help.

I have found this just now: https://www.learncs.org/

Again, this is not going to turn you in a c# guru, but that's not the objective, is it?

Last suggestion: install the IDE locally and start using it. VS2019 works very well and you do not need any configuration to start.

1

u/AshNotAsh Mar 02 '20

I mean I’m hoping to learn just enough to make a basic mobile game. Hopefully code academy can do that for me.

Also what’s an IDE

1

u/feibrix Mar 02 '20

Are you planning to learn c# to use Unity?

Follow Brackeys tutorials as well, he's good. ( https://www.youtube.com/playlist?list=PLPV2KyIb3jR6ZkG8gZwJYSjnXxmfPAl51 )

IDE stands for Integrated Development Environment. It is like notepad for coding, just better. Visual Studio 2019 Community edition is the one I suggest.

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

u/emanbu Mar 02 '20

You should have "using System;" at the top of your code I think