C# Methods – Easy Guide
Visual Programming | C# Methods
What is a Method?
A method is a block of code that only runs when you call it. You can send information (called
parameters) into it. Methods help you reuse code — write it once, use it many times.
How to Create a Method
To create a method, give it a name followed by (). Here's the basic structure:
class Program
{
static void MyMethod()
{
// code to be executed
}
}
• MyMethod() – the name of the method
• static – means the method belongs to the class, not an object
• void – means the method does not return any value
How to Call a Method
To run a method, write its name followed by () and a semicolon. Example:
static void MyMethod()
{
[Link]("I just got executed!");
}
static void Main(string[] args)
{
MyMethod();
}
You can also call the same method multiple times:
MyMethod();
MyMethod();
MyMethod();
Parameters and Arguments
You can pass information into a method using parameters. Parameters work like variables
inside the method.
Example – passing a name into a method:
static void MyMethod(string fname)
{
[Link](fname + " Refsnes");
}
static void Main(string[] args)
{
MyMethod("Liam");
MyMethod("Jenny");
MyMethod("Anja");
}
📝 Note: fname is the parameter. Liam, Jenny, and Anja are the arguments (the actual
values passed in).
Multiple Parameters
You can use more than one parameter, separated by commas:
static void MyMethod(string fname, int age)
{
[Link](fname + " is " + age);
}
static void Main(string[] args)
{
MyMethod("Liam", 5);
MyMethod("Jenny", 8);
MyMethod("Anja", 31);
}
📝 Note: The number and order of arguments must match the parameters exactly.
Default Parameter Value
You can set a default value for a parameter using =. If no argument is passed, the default value
is used:
static void MyMethod(string country = "Norway")
{
[Link](country);
}
static void Main(string[] args)
{
MyMethod("Sweden");
MyMethod("India");
MyMethod(); // Uses default: Norway
MyMethod("USA");
}
📝 Note: A parameter with a default value is called an optional parameter.
Return Values
If you want a method to give back a result, use a data type (like int or double) instead of void,
and use the return keyword:
static int MyMethod(int x)
{
return 5 + x;
}
static void Main(string[] args)
{
[Link](MyMethod(3)); // Output: 8
}
Returning the sum of two parameters:
static int MyMethod(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
int z = MyMethod(5, 3);
[Link](z); // Output: 8
}
📝 Note: Storing the result in a variable (like z) is recommended — it's easier to read and
manage.
Method Overloading
Method overloading means having multiple methods with the same name but different
parameters. This avoids creating separate method names for similar actions:
int MyMethod(int x)
float MyMethod(float x)
double MyMethod(double x, double y)
Example – without overloading (two different names):
static int PlusMethodInt(int x, int y)
{
return x + y;
}
static double PlusMethodDouble(double x, double y)
{
return x + y;
}
Example – with overloading (same name, cleaner code):
static int PlusMethod(int x, int y)
{
return x + y;
}
static double PlusMethod(double x, double y)
{
return x + y;
}
static void Main(string[] args)
{
int myNum1 = PlusMethod(8, 5);
double myNum2 = PlusMethod(4.3, 6.26);
[Link]("Int: " + myNum1);
[Link]("Double: " + myNum2);
}
📝 Note: Multiple methods can share the same name as long as their parameters are
different in number or type.