
In the C # programming language, methods, also known as functions, are code pieces that can be used for the purpose of being called later in any part of the program.
Methods not only add flexibility to programs, but also prevent us from repeating the same code over and over.
For example, let’s output a message window when a repetitive operation is performed in an application. For this, let’s define a method instead of writing different codes at the end of each operation.
By calling the method where we want to be used, we can perform the operation without having to generate various codes.
There is no need to write the same codes over and over, and the number of codes is reduced, making the program easier to read. Thus, it saves time and gives the program a structured appearance.Methods do not make sense on their own and do not work, they must be called from within the program to work.
There are details that we need to know to define a method:
- Access tokens: In the access token, it is used to determine whether we can call from other parts of the program or not.
If we want the program to be callable from anywhere, the “public” access specifier should be used.
If no access token is specified, the default access token “privete” is used. This access specifier is used only inside the class in which the method is written. - Return type: If we want it to return value, we can set the return type as int, string etc. If we want to return a value, the return command is used. If we are going to use a method that does not return a value, we need to use void .
- Method name: It is defined in the same way as we normally define a variable name.
- Parameter (argument) List: There may not be a parameter in the parameter list. If there is a parameter, it is written directly. If there is more than one parameter, it should be entered together with the parameter type, separated by a comma (,). The use of parameters is used to call the data necessary for the method to work.
Parameters are the information sent from the function from which the method wants to receive data during operation.
For example;
int MethodName(int x,int y)
{ return x+y; }
C# Method (Function) Examples
Example 1
=>Example of method that does not return a value:
write static void()
{
Console.WriteLine(“Hello Sydney”);
}static void Main(string[] args)
{
summer();
}
=>If we want to use the method in this example more than once, we use it as follows.
write static void()
{
Console.WriteLine(“Hello Sydney”);
}static void Main(string[] args)
{summer();
summer();
summer();}
Example 2
=>Example of function that can take parameters but return no value:static void info(string name)
{Console.WriteLine(“Welcome”+name);
}
static void Main(string[] args)
{Console.WriteLine(“Enter Your Name”);
string name = Console.ReadLine();info(name);
}
Example 3:
=>Example of function that can take parameters and return values:
static int square_area(int number)
{calculate int=number*number;
calculate return;
}
static void Main(string[] args)
{Console.WriteLine(“Enter a side of the square.”);
int edge= Int32.Parse(Console.ReadLine());int result=square_area(edge);
Console.WriteLine(result);
}