Methods or functions are the collection of statements which gets executed when the method is called. Any method gets executed when it is called in the Main() method.
Any method has three stages,
Method declaration,
Method definition,
Method Calling
Example of a method in C#,
using System;
namespace Method{
class Program{
// Method declaration.
public void greet();
//Method defination.
public void greet(){
Console.WriteLine("Hi");
static void Main(string[] args)
{
// Method Calling.
greet();
}
}
}
}
Methods with Parameters:
Methods can also be made with parameters. This type of methods can accept arguments.
using System;
namespace Method{
class Program{
/* Syntax,
public return type(parameters)
{
Statements...
} */
public void add(int a){
int sum = a+a;
Console.WriteLine("Sum is: ",sum);
}
static void Main(string[] args)
{
add(5);
}
}
}