C# Methods - Simple English Guide
What is a Method?
A Method in C# is a block of code that performs a specific task. Instead of writing the same code many times, you
create a method once and call it whenever you need it.
Why Do We Use Methods?
• To avoid repeating code.
• To make programs cleaner and easier to read.
• To organize code into small parts.
• To make debugging easier.
General Syntax
access_modifier return_type MethodName(parameters)
{
// code
}
Parts of a Method
1. Access Modifier
Usually public or private.
2. Return Type
Determines what the method returns.
Examples: void, int, double, string, bool.
3. Method Name
The name used to call the method.
4. Parameters
Inputs sent to the method.
Void Method Example
static void Hello()
{
[Link]("Hello");
}
Calling a Method
To use a method, we call it:
Hello();
Methods with Parameters
static void PrintName(string name)
{
[Link](name);
}
PrintName("Ahmed");
Parameter vs Argument
Parameter: variable inside the method definition.
Argument: actual value sent to the method.
Methods with Return
static int Sum(int x, int y)
{
return x + y;
}
What Does return Mean?
The return statement sends a value back to the place where the method was called.
Using Returned Values
int result = Sum(5, 3);
[Link](result);
Why return is Powerful
Returned values can be:
• stored in variables
• printed
• compared in conditions
• used in calculations
What is static?
A static method belongs to the class itself, not to an object.
Example:
static void Hello()
{
}
Why Main is static
The program starts from Main() automatically, so it must be static.
Flow of Execution
The program starts from Main().
When Main calls another method, execution moves to that method.
After finishing, execution returns back to Main.
Variable Scope
Variables created inside a method can only be used inside that method.
Method Overloading
You can create multiple methods with the same name if they have different parameters.
Example:
static int Sum(int x, int y)
{
return x + y;
}
static int Sum(int x, int y, int z)
{
return x + y + z;
}
Common Mistakes
• Forgetting static.
• Forgetting return.
• Wrong return type.
• Calling a method incorrectly.
How to Think About Methods
Ask yourself:
1. What inputs does the method need?
2. What should it return?
3. What task should it perform?
Practice Ideas
Try creating methods for:
• Finding the maximum number
• Finding average
• Converting temperature
• Checking even/odd
• Rectangle area
• Circle area
• Prime number check