C# Beginner course
OmN3xin (4)

Why C#?

C# is a simple language that popular game engines use such as Unity or Godot.

Variables

In C#, you make variables by declaring what they are, then naming them. For instance, an integer in

int

here are the different variables types:
integer =

int

string =

string

character =

char

float =

float

double =

double

boolean =

bool

these are the main variable types.
To finish declaring a variable you enter the name the a semicolon. Like this:

 /*type*/int /*name*/integer;

If you want to, you can set the value right there like this:

 int cookies = 5;

Comments

To write a single line comment you write "//".
For a multi line comment you type

/* 

to start and

 */

to end.

Changing Variables

To increase int by one you write

variable++;

To decrease you write

 variable--;

To increase by an amount you write

 variable += /*amount*/5;

And decrease:

 variable -= /*amount*/5;

You can also add strings together by writing "+" in between strings.

Input and Output

To print something to the console you write

 Console.WriteLine(/* what you want the console to say*/);

To get input from the user you write

 Console.ReadLine();

You can store this in a string like this:

 string input = Console.ReadLine();

If statements

An if statement is when you see if something is true or false.
An if statement is write like this:
First write "if" to declare that this is an if statement.
Next write brackets and write your condition inside. Like this:

 if(1 > 2)

Then write curly brackets and write you want to happen inside there.
If you want to do the opposite of the condition, the write and explanation point at the beginning of that statement like this:

 if(!1>2)
{
}

You can also do an if else statement where you check if something is true or false, and if its not, then the program fires the else statement.
They look like this:

 if(1>2){
Console.Write("this is impossible");
} else{
Console.Write("this is possible");
}

Methods

A method is basically a lot of code stuffed into a command and you can call it.
To create a method you type your return type (just use "void" for now), then your method name, then brackets, and last but not least curly brackets. It Should look something like this:

void MyMethod()
{
}

You put all your code inside the curly brackets.
to call the method, you write the name, then brackets, then semicolon.

 MyMethod();

Converting Variables

To Convert a int into a string, you type the variable name and then

 ToString();

So:

 int number = 5;
string newString = number.ToString();
Console.WriteLine("The new string is "+newString);

to convert an int into a string you type

int result = Int32.Parse(intToConvert);

Calculator

At last we are going to make a program!
To start off, delete the "Console.WriteLine("Hello World");" line. We are going to need two integers, one for the first number typed and one for the second.Type this:

int num1;
int num2;

inside the main method.
Now lets add some console stuff.

int num1;
int num2;
Console.WriteLine("Write your first number");
/* read the line */ num1 = /* change string to int */ Int32.Parse(Console.ReadLine());
Console.WriteLine("Write your second number");
/* read the line */ num2 = /* change string to int */ Int32.Parse(Console.ReadLine());
// and to sum it off tell them there answer.
int anwser = num1 + num2;
Console.WriteLine("your answer is "+anwser);

I hope this tutorial helped you learn the basics of c#!
If you want me to do a tutorial about more advanced c#,
then comment it bellow!

You are viewing a single comment. View All
Wumi4 (454)

Just fixing: By default, Godot uses it's GDScript for scripting, not C#. You can use C#, but it will require you to do some stuff.

OmN3xin (4)

@Wumi4 I know, my point was that you can. Even though the default is GDscript.