Methods and Parameter Usage in C#
Learn how to define methods and use parameters in C#, including value and reference parameters, optional parameters, and examples.
In C#, methods are used to organize and reuse repetitive code. A method performs a specific task and can be called many times within a program. This helps avoid code duplication and improves readability.
Defining and Calling Methods
A method is defined with a return type, a name, and optional parameters. For example, the following method prints a simple message:
static void Greet()
{
Console.WriteLine("Hello!");
}
static void Main()
{
Greet(); // method call
}
// Output:
Hello!
Methods with Parameters
Methods can receive values from outside. These are called parameters. In the example below, a name parameter is taken and the greeting message is personalized:
static void Greet(string name)
{
Console.WriteLine("Hello " + name + "!");
}
static void Main()
{
Greet("Alice");
Greet("Bob");
}
// Output:
Hello Alice!
Hello Bob!
Methods Returning Values
Methods can perform operations and return a result. For this, a return type is defined (int, string, etc.).
The following example shows a method that adds two numbers:
static int Add(int a, int b)
{
return a + b;
}
static void Main()
{
int result = Add(5, 7);
Console.WriteLine("Sum: " + result);
}
// Output:
Sum: 12
Default Parameters
Parameters can have default values. If no value is passed during the call, the default value is used:
static void PrintMessage(string text = "Default message")
{
Console.WriteLine(text);
}
static void Main()
{
PrintMessage("Hello world!");
PrintMessage();
}
// Output:
Hello world!
Default message
Parameter vs Argument
A parameter is the name used in the method definition. An argument is the actual value passed when calling the method.
static void Square(int number) // parameter: number
{
Console.WriteLine(number * number);
}
static void Main()
{
Square(4); // argument: 4
}
// Output:
16
Overloading
Methods with the same name but different numbers or types of parameters can be defined. This is called method overloading:
static int Multiply(int a, int b)
{
return a * b;
}
static int Multiply(int a, int b, int c)
{
return a * b * c;
}
static void Main()
{
Console.WriteLine(Multiply(3, 4));
Console.WriteLine(Multiply(2, 3, 4));
}
// Output:
12
24
Recursion
When a method calls itself, it is called recursion. Each recursion must have a base case; otherwise, the loop will continue infinitely.
static int Factorial(int n)
{
if (n <= 1)
return 1; // base case
return n * Factorial(n - 1); // recursive call
}
static void Main()
{
Console.WriteLine(Factorial(5)); // 120
}
ref and out Parameters
In C#, the ref and out keywords allow parameters to be passed by reference. This means changes made inside the method are reflected outside.
// ref example
static void Increment(ref int number)
{
number++;
}
// out example
static void ReadValue(out int result)
{
result = 42;
}
static void Main()
{
int x = 5;
Increment(ref x);
Console.WriteLine(x); // 6
int y;
ReadValue(out y);
Console.WriteLine(y); // 42
}
Multiple Parameters with params
The params keyword allows methods to take a variable number of parameters.
You can pass either an array or individual values.
Example: Summing Numbers
static int Add(params int[] numbers)
{
int sum = 0;
foreach (int n in numbers)
sum += n;
return sum;
}
static void Main()
{
Console.WriteLine(Add(1, 2, 3)); // 6
Console.WriteLine(Add(5, 10, 15, 20)); // 50
}
Example: Joining Strings
static string JoinWords(params string[] words)
{
return string.Join(" ", words);
}
static void Main()
{
Console.WriteLine(JoinWords("C#", "is", "a", "powerful", "language."));
// Output: C# is a powerful language.
}
Local Functions
Starting from C# 7.0, methods can be defined inside another method. These are called local functions. They help write cleaner and more organized code.
static void Main()
{
int Multiply(int a, int b) // local function
{
return a * b;
}
Console.WriteLine(Multiply(3, 4)); // 12
}
TL;DR
- Methods are reusable blocks of code.
- Parameters are used to pass data into methods.
returnis used to return a value.- Default parameters make code flexible.
- Overloading allows defining methods with the same name but different parameters.
refandoutparameters allow modifying values by reference.paramsenables variable numbers of parameters.- Local functions are helper methods defined inside another method.
- Recursion means a method calling itself to solve a problem.