Welcome to the methods section.
In this section we are going to cover methods and have a look at what they do and how we can use them.
And they are super important in any object oriented programming language.
And we're going to start off by checking out the definition, which is from the official documents from
Microsoft.
A method is a code block that contains a series of statements.
A program causes the statements to be executed by calling the method and specifying any required method.
Arguments.
In C sharp, every executed instruction is performed in the context of a method.
The main method is the entry point for every C sharp application, and it is called by the Common Language
Runtime CLR when the program is started, and so far we have seen one method that does exactly that.
It's the main method, and it starts whenever we start our program.
So we have used that and we have not created our own method yet, but we will do so in the next videos.
But first of all, let's have a look at what the syntax for a method looks like.
So you have the access specifier.
Then the return type, the method name.
Then in parentheses you have the parameter list.
And then finally you have the method body.
And as usual when creating classes methods, as I explained earlier, you have the opening curly bracket
and the closing curly bracket.
So that's the syntax.
But what are all of these things?
What is an access specifier?
What is a return type?
What's a method name and so forth.
And there is a great instruction on this on tutorials point, and they have given a great explanation
on the access specifier.
It determines the visibility of a variable or method from another class.
That means that when you create multiple classes, you can access methods that have a specific access
specifier and cannot access others.
So there are multiple types of access specifiers.
For example, public.
If you use public, you can access that method from another class.
If you use private, for example, you cannot.
And there are other access specifiers and we're going to cover them later on in the course, then return
type.
A method may return a value, so it may.
It doesn't have to.
The return type is the data type of the value that the method returns.
So that means that if you want the method to give back something, so return something.
You have to specify which data type you wanted to return.
So for example, if you want to return a number then you could use for example integer.
If the method is not returning any values, then the return type is void.
So there are cases where a method should just run some code and shouldn't return anything.
Then you would simply use void instead of integer or string or double or something similar.
Then you have the method name.
The method name is a unique identifier and it is case sensitive.
It cannot be same as any other identifier declared in the class.
So you have to create a name that is specific and unique, and then in this case sensitive in in the
sense that you can use capital letters and so forth.
But what is best practice is that you start off with a capital letter, and then you usually use lowercase
letters.
And except you have for example, drive car.
Then you would write the c as a capital letter as well.
Then you have the parameter list enclosed between parentheses.
The parameters are used to pass and receive data from a method.
The parameter list refers to the type, order, and number of the parameters of a method.
Parameters are optional.
That is, a method may contain no parameters, which means that there is an input to the method and
those are the parameters.
And you can put something into the method and you can get something back.
And that's the return type.
So and we will have a look at that in a minute.
So we'll have an example where you can see the parameter list and the return type.
And then the method body.
This contains the set of instructions needed to complete the required activity.
And yeah that's pretty much simply what the method should execute.
So a method should execute some code.
And whenever you call that method it should always execute that code.
So let's have a look at an example.
So we have here an example where I created a method which says public int add.
So public is the access specifier which means this method can be used from other classes as well.
For now, it doesn't really make a difference for us because we haven't created multiple classes yet,
but we will do so later on.
Then you have int which is a return type.
So this method will return an integer which is a number.
Then a name of the method.
In this case add.
So this method should simply add two values and the two values it should add are the parameters.
So it's int num1 and int Num2.
And what's important is that you add the data type.
Then you enter the name of the parameter.
You separate them by comma and then you go ahead.
So you have you can have multiple different parameters.
It doesn't have to be two.
It could be one, it could be 50 whatever.
So what usually is the case that you don't use more than let's say 5 or 6.
But it can be as many as you want.
All right.
And then what you do here is you have two numbers and then the parentheses is closed.
And you open your curly brackets.
And within the curly brackets you have the method content or the method body.
And in here you run the code.
So whenever you have a data type which is for example an integer, you have to return something that
is of that data type.
So in our case our return result and result is an integer which is equal to num1 plus num2.
So this method is a simple example of just a method that will add two values and return the result of
those two values.
So or the sum of those two values.
So you see this is a simple example.
Then you can even reduce the code a little.
So you could get rid of this additional method.
You could simply say okay return Num1 plus Num2.
So you won't need that additional step in there.
But you can of course go ahead and create that just for easier reading, for example.
But we will have a look at multiple different examples in the next video.
So in the next video we're just going to create a void example which doesn't return something.
And then we'll have a look at methods that do return something and so forth.
All right.
So that was just the theoretical part.
Let's go to the demo and let's have a look at what we can do with methods and what the purpose is.