C #: from the basics to the medium level (variables and methods)
This is the first of many articles about C#.
We try to learn to use Unity, and Unity use C#.
In my personal experience, I think to know the basics of this language make our work in Unity much better and easy to understand.
I will not talk about CLR, JIT or Assembly, this is out of my scope of “GameDev”, but if you are interested there are many great guide about it.
I try to focus on basics, like Classes, Constructors, Abstraction, Delegate — Event, Interfaces.
Then I focus on Collections, because I think is one of the most important topic, and I go deep again with Array and List, then pass to ArrayList, Dictionary, LinkedList and more advance concept.
At the end we talk about design pattern.
If we can master THESE THINGS, we start to think like real Software Engineers.
At least, this is what i’m think.
So, let’s get started!
First of all we have already see variables and methods in Unity. Here is exactly the same. Just know that we don’t have Inspector, so every time we create a variable we need to initialize directly in code.
Don’t worry about green underline, is because we have all this variables, but we don’t use yet.
In Unity we have two methods :
We use them for print something in the console.
Here we have another method
As we can see above, inside we pass a classic beginner string “Hello World”
We can pass every variables we have
I use here five types : int, float, string, bool, char.
Here is a documentation abou all the types in C#
Built-in types - C# reference
The following table lists the C# built-in value types: The following table lists the C# built-in reference types: In…
As always we save the project before run with Ctrl+S.
If we press here
The application is executed, we open the Console, we see every variable we have and if we press Enter the application closes.
As we can see it works little bit different from Unity.
The methods work in the same way
Why we have an error?
As we look, the method Main is static.
If we change public to static
now we are ok.
After we talk in more details about static keyword.
If we run
It runs the code, and if we press enter Application closes.
Last thing before move forward
If we need a variable that be equal for all the time of our application / project / game, we can use the keyword const.
For example, if we need a number for some operation, and we need is always the same, we can declare it like this
If we try to change its value
We have an error, because number is declare as const.