Learning C# Notes - Part IV: Methods
/Here are my notes on eveyrthing related to methods.
Methods Basics
We can think of methods as the different actions that our classes can perform so they are the basic building blocks that determine how a class behaves.
Sometimes they are called ‘Functions’.
We first define a method using paretheses () after the name. Method names usually start with a capital letter.
After defining the method, we can call it from somewhere else.
See an example below. We first define a method and then it is called from some other method definition.
class Program { void FirstMethod() { // code to be executed } void OtherMethod() { //Some More code FirstMethod(); } }
Return Types & Passing Parameters
Methods can return a variable after they run, which needs to be specified when we define the method.
If a method returns no value, it will be of type void.
While return types are like outputs, we can also input information into a method. These inputs are called parameters.
For a method to take parameters, we must define them when we define the method.
A method can take multiple parameters.
We can give parameters a default value by using the equals sign when defining the method.
The ‘params’ keyword can also be used to make a parameter optional.
To be precise, it allows you to pass any amount of elements in an argument's array and if you pass 0 elements this is in practice like making the argument optional.
The params keyword can only be used once per method and must be the last argument.
int SumNumbers(int num1, int num2) { return num1 + num2; } void MyMethod(string country = "Norway") { Console.WriteLine(country); }
Method Overloading
Multiple methods can have the same name and vary in how many parameters they take and even their return types.
This is useful when you need a method that can perform generally the same function but using different types of information.
We just define and call them as normal using the same method name.
See the example below. Both methods will just sum numbers but they can take different type of variables as values.
int SumNumbers(int num1, int num2) { return num1 + num2; } float SumNumbers(float num1, float num2) { return num1 + num2; }
Calling Method from other classes & Static Methods
When working in Unity with C#, most of the time, each class lives in its own script file.
Is very common for a class to call another class’ method.
This is usually done by typing the name of the class followed by a full stop and then the name of the method, ending in paretheses.
If we want to call a method within a class without instatiating that said class, a possible solution could be to make the method static when defining it.
Unity Methods
Unity includes some important methods that we can use on Monobehaviour type scripts.
Awake() is the first method called and it should usually be used for initializing things. Is only called once.
Start() is also called once after Awake() but inly if the script component is enabled.
Update() is called on every frame that the script component is active.
LateUpdate() is called every frame but only after everything on Update() has finished executing.
FixedUpdate() is the same as above but it has the frequency of the physics system.
OnEnable() is called when the gameobject containing the script is enabled and becomes active.
OnDisable() is called when the gameobject containing the script is disabled or destroyed.