Methods or Functions in C#
In C#, Methods and functions are both the same thing. In the article, we will learn everything about this topic. We will learn how to define a method in C#, what method is used for, how to call a method, and what different keywords are used with a method in C#.
Method
- A method is a group of statements that together perform a task.
- Methods are functions declared in a class and may be used to perform operations on class variables
- They are blocks of code that can take parameters and may or may not return a value.
- It is used to perform specific task.
- Methods are reusable.
{
//Declaring a method
public void MyMethod()
{
//method body
}
}
{
//Declaring a method
public void MyMethod()
Console.WriteLine("My name is Method");
static void Main(string[] args)
Program program = new Program();
Console.ReadLine();
}
}
{
//Declaring a method
public void MyMethod()
Console.WriteLine("My name is Method");
static void Main(string[] args)
Program program = new Program();
program.MyMethod(); //Calling method
program.MyMethod(); //Calling method
program.MyMethod(); //Calling method
program.MyMethod(); //Calling method
Console.ReadLine();
}
}
namespace SoftwareEngineerDotNet
class Program
{
//Declaring a method with multiple parameters
Console.WriteLine("Your name is "+ name);
static void Main(string[] args)
Program program = new Program();
program.MyMethod("John", 40); //Calling method with multiple arguments
Console.ReadLine();
}
}
}
Many parameters should be separate with commas.
Method calls must have the same number of arguments as there are parameters and the arguments must be passed in the same order and datatype.
What is default parameter value in C#
You can create a default parameter value using the equals sign ( = ). It is also known as an optional parameter. If you call the method without an argument then it will use default value.
Method Return Values
Method may or may not return a value, we used void in above exmaple which indicates that the method should not return a value. If you want method return any value you can use primitive data types like int, double and use the return keyword inside the method.
Below is an example of two data parameters with return type method
{
//Declaring a method with the return value, multiple parameters
public int Sum(int x, int y)
return 10 + x + y;
static void Main(string[] args)
Program program = new Program();
Console.ReadLine();
}
}
Below is an example of two data parameters with return string but the return type is int
{
//Declaring a method
public int Sum(int x, int y)
return x + y + "Amit"; //Compile time error
static void Main(string[] args)
Program program = new Program();
Console.ReadLine();
}
}
Below is an example of a method where you can store the result in a variable.
{
//Declaring a method
public int Sum(int x, int y)
return x + y;
static void Main(string[] args)
Program program = new Program();
//Store the result in a variable
int z = program.Sum(5, 10);
Console.WriteLine(z); //Calling method
Console.ReadLine();
}
}