In programming, a method is a block of code that performs a specific task or function. Methods are an essential concept. In other coding languages, they are sometimes called functions. They are used to eliminate the need to copy paste code, consequently, reducing the complexity of a program. Here are the main parts of a method:
The method signature defines the name, return type, and parameters. For now, at the start of all our methods, we will add public static. Later on, we will learn the meaning of what public and static means. Here is an example...
public static int Add(int a, int b)
int is the return type. This means that the method should return a number. Methods can also have no return if the method is void.
Add is the method name. The method name should be descriptive and accurate. In this case, the method is adding two numbers which is why we are returning a number.
(int a, int b) are the parameters. We know they are two integer values based on the data type provided. These parameters can only be accessed inside the method.
The method body is enclosed in curly braces and contains the actual code that defines the method's behavior. This code can include statements, variable declarations, loops, conditionals, and more. The method executes its code when it is called or invoked.
Continuing from our Add method above, the method signature + method body will look something like this.
public static int Add(int a, int b)
{
int result = a + b;
return result;
}
You can also combine the two lines to make it look a tiny bit prettier.
public static int Add(int a, int b)
{
return a + b;
}
If a method has a return type other than void, it should use the return statement to specify the value that it will return to the caller. The return statement can only be used in methods with a non-void return type. Once the return statement is executed, the method exits and returns control to the caller.
In practice, this is what using the add method might look like.
namespace MyFirstProject
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(Add(2,4));
}
public static int Add(int a, int b)
{
return a + b;
}
}
}
Return
6
As you could probably see from the above code, in order to call(or run) the method, it is:
nameOfTheMethod(any parameters the method might take in);
Some methods may require a class in front of the method or instance variable of a class.
Example of method
Console.WriteLine()
You can also call methods on objects(also known as reference types), such as Strings. We will learn more about the difference between the two in the OOP section. For now, it's important to learn how to create methods and call the methods.
string message = "HELLO";
message.ToLower(); // returns "hello"
C# has a lot of methods built in so that users do not have to reinvent the wheel. Later on, I teach you how to manipulate strings and different types of data. I also recommend going onto Google to see if a method already exists for what you want to do. Some good resources are GeeksForGeeks and W3 Schools.