Chapter 3: Your first code.
Now that we'd got everything set up, it's time to start with the fun stuff.
first off, open your terminal, you should know how to do that by now.
afterward, create a new directory anywhere you like, let's say I want a directory called projects then I put my game under audiogame.
mkdir projects
cd projects
mkdir audiogame
cd audiogame
now you might be a bit overwhelmed if you have no experience with command line previously, don't worry it's simple.
- mkdir creates a directory.
- cd changes your current directory, by default, when you open your terminal, it goes to your home folder, for example, /users/mohamed.
now that we created the directories we need, let's create our C# project, a project is where all your source code is
dotnet new console
now, if you check the folder where we did put our project, projects/audiogame, or wherever you decided to, you'll find a few things.
- obj
- audiogame.csproj
- Program.cs
not much to be concerned about right? Yep, simple, in fact, it's less than that.
obj is a form of cache that the compiler and package manager generates, so ordirnarily we never touch it, so you can forget about it entirely.
audiogames.csproj is where our project configuration goes, and where we could define a phew things, for now, we don't need to touch this.
Program.cs is what we care about, open it, it should look like.
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
if you have prior experience with older versions of c# you might be taken a back because this is way less than you've expected, but the link says it all
The features that make the new program simpler are top-level statements, global using directives, and implicit using directives. The term top-level statements means the compiler generates the class and method elements for your main program. The compiler generated class and entry point method are declared in the global namespace. You can look at the code for the new application and imagine that it contains the statements inside the Main method generated by earlier templates, but in the global namespace. You can add more statements to the program, just like you can add more statements to your Main method in the traditional style. You can access args (command-line arguments), use await, and set the exit code. You can even add functions. They're created as local functions nested inside the generated entry point method. Local functions can't include any access modifiers (for example, public or protected).
too much blah blah to basically say that you can now put code in Program.cs and treat it like a Program class and main method exist. Similar to python, etc.
if you have no experience with C# and you're confused, you don't need to worry, this is unimportant, it's just a simple clarification, and it won't affect you.
the, //, in C# is a comment, comment is text in the middle of code that doesn't affect the program, it's useful for documentation, or it could be a todo, or a reminder, or anything, basically, having it or not having it doesn't affect the final program, and gets completely ignored by the compiler, but is useful because it's extra information for possibly confusing code, etc.
now, let's explain this line
Console.WriteLine("Hello, World!");
classic, hello world pretty much is a standard for a learning a language, but let's explain it step by step since there are people who have never coded before.
- Console is a class, think of a class as something that holds data and can perform operations. It's like a folder with text files and executable files that when clicked can do things.
- the . means that we're accessing something inside that class, again think like how you access what's inside a folder with /
- WriteLine is the name of a method, it's like a reusable command that performs a specific action, in this case, output text to the terminal.
- the ( (left paren) is the start of a method's argument list. Method arguments are like the details you tell the method, in this case, we tell it what text do we want to display.
- "Hello, World!", is a string, string is basically text, and this is the argument we give the method, as said above. A string is always wrapped between two " (quotation marks)
- the ) (right paren), We are done giving the method arguments, so we close the argument list.
- the ; (semicolon), is something we use when this line have ended. We already called the method from the Console class, given it our arguments, an closed it, now, we need to tell the compiler we are done with this line, so we put a semicolon.
and that's it, now, try running it.
$ dotnet run
you should get a message that says Hello, World!
That's it, you can go play around with the text displayed, or you can go add an extra WriteLine, or you could try removing something and see what happens.