How To Learn C# From No Experience - Biendeo/My-Clone-Hero-Tweaks GitHub Wiki
Intro
The easiest way to understand how to make a Clone Hero mod is to have a good understanding of the fundamentals of computer programming. This wiki page aims to be a crash course into the fundamentals of computer programming.
What every program does
To put it simply, every program is effectively a starting set of conditions and an ending set of conditions. There is some state your computer is in before the program starts, and a different state when the program ends. On top of that, we can say that two programs can be sequentially composed if the post-condition of the first program matches the pre-condition of the second program, therefore creating a new program which has the pre-condition of the first program and the post-condition of the second program.
This explanation is very abstract, but it's useful just because it encourages you to think of computer programs as building blocks and micro-problems. Every problem often consists of sub-problems and primitive (can't be broken down further) operations. Your ideas will always be high level abstract tasks that you'll have to break down into smaller problems, which can be broken down further until they eventually become described using concrete operations.
A basic hello world program
using System;
namespace HelloWorld {
public class Program {
public static void Main() {
Console.WriteLine("Hello world!");
}
}
}
Above is what is referred to as a "Hello world" program; a simple program that when run prints "Hello world!" to the terminal. It's a classic way of describing the basic starting point to creating a program.
First thing to note is that C# does not treat whitespace (tabs and spaces) as anything other than a separator for terms. Instead, whitespace is often a stylistic thing for us programmers to help make the program more readable. It's a good idea to follow these style conventions, but your program will work regardless, and it's fair to experiment with what you prefer in the end.
Let's go line by line. The first line is using System;
. It's important to note that ;
means the end of a statement, so in order to continue after using System
, or any other line, you'll need to use a ;
for the most part. The exception is code blocks (pairs of {
and }
, but more will be said about those later. The meaning of using System
is just that any time we refer to something, the program should check the System
namespace if that term exists. This'll be explained in a little bit.
The next line is namespace HelloWorld
. A namespace is a way to group up your code so that the names of your classes don't collide with the names of other people's classes. Generally the namespace is either a company or a project name. In this case, the project is called HelloWorld
, so it's a good idea that all of our code is in this namespace. A namespace is a block, so everything inside this namespace is inside the {
and }
characters afterwards.
Afterwards is public class Program
. The name of the class can be called anything, I've just called this Program
because I want it to indicate that it contains the start of the program. C# is an object-oriented language, so all fields (data), and methods (behaviour) must exist inside classes. Again, a lot more detail will be given later. For now, it's something we need to set up. The public
aspect to it means it can be seen by everything, including other programs. Lastly, classes are blocks, just like namespaces, so everything is in a {
and `} block. It's important to note that this line is inside a code block, so by style convention you indent these by "one level". An indent level is often either 4 spaces or 1 tab character. It's preference which one you use, but it's good to be consistent with the rest of the file you are editing.
Next is the meat and bones of the program: public static void Main(string args[]) {
. This is a method called Main
, which returns void
, and is public
and static
. It also takes no arguments (those would be defined inside the (
and )
characters. That's a lot to digest in one go, but ultimately what this is doing is defining a method, which is behaviour or actions. It's static
, so it exists without needing a running object associated (more on that later). It's public
so it can be seen from outside the program. Methods accept zero or more arguments, and then, after running their behaviour, evaluate as the return type. That sounds a bit complex, but in this case, since the return type is void
, that just means there's no value, and there's no arguments so nothing is passed in. To put it simply, this program (theoretically) runs the same every time and evaluates as no value. Again, note this is inside two code blocks, so this line is indented by two levels now.
Finally, we're at Console.WriteLine("Hello world!");
. Microsoft ships C# with .NET Framework, a common library that contains a lot of useful tools. In it is the System
namespace, which has the Console
class, and the WriteLine
static method, so we can call that method as such. To refer to this method, we use the .
character, so System.Console.WriteLine
. We don't need to say System
though, because of the using
statement above, so all we need is Console.WriteLine
. Since this is a method, we use (
and )
to call it, and inside it are the values of what get passed in. In this case, it is just the string "Hello world!"
. A string is just a sequence of characters, and you can quickly write them with the "
character. So we call this method and pass the string "Hello world!"
in. It then does the magic of putting that string onto the terminal. Since this is an actual statement, it ends in the ;
character. Again, note we're three indents in.
Lastly, we need to close all the blocks we've entered. Stylistically, these exist on their own lines directly underneath the first character of the line that opened them. The first }
corresponds with the last {
that appeared, in this case, the method's curly brace. The second one corresponds with the curly brace of the class declaration. Lastly, the last one corresponds with the curly brace of the namespace line.
And all of that explains the simple hello world program. It's a lot to take in at first, but it's a good short program to refer back to if you're struggling at first. If you've never programmed before, the syntax is going to look very odd, and that's because C# is based strongly on the syntax of C, a programming language made in 1972 which was designed to easily translate into machine code. However, don't be too overwhelmed, because most programming languages do not have too many concepts to grasp. After playing around with them a bit, you should be able to get accustomed to the syntax and start creating ideas of your own.