Methods in C# - Textnotes

Methods in C#


Methods are blocks of code that perform a specific task. They help in reusability, modularity, and readability of code.

1. Defining and Calling Methods

Defining a Method


using System;

class Program
{
// Method definition
static void GreetUser()
{
Console.WriteLine("Hello, Welcome to C# Methods!");
}

static void Main()
{
// Method call
GreetUser();
}
}

Explanation:

  1. static void GreetUser() → Method with no return value and no parameters.
  2. GreetUser(); → Calling the method inside Main().

2. Method Parameters and Return Types

Methods can accept inputs (parameters) and return values.


using System;

class Program
{
static int AddNumbers(int a, int b) // Method with parameters and return type
{
return a + b;
}

static void Main()
{
int sum = AddNumbers(10, 20); // Call method with arguments
Console.WriteLine("Sum: " + sum);
}
}

Explanation:

  1. int a, int b → Parameters.
  2. return a + b; → Returns the sum to the caller.
  3. Method return type must match the return value type.

3. Method Overloading

Method overloading allows multiple methods with the same name but different parameters.


using System;

class Program
{
static int Multiply(int a, int b) => a * b;
static double Multiply(double a, double b) => a * b;

static void Main()
{
Console.WriteLine(Multiply(2, 3)); // Calls int version
Console.WriteLine(Multiply(2.5, 3.5)); // Calls double version
}
}

Explanation: Compiler decides which method to call based on parameter type or number.

4. Optional Parameters

Parameters can have default values, making them optional during method call.


using System;

class Program
{
static void Greet(string name = "User")
{
Console.WriteLine("Hello, " + name);
}

static void Main()
{
Greet("John"); // Hello, John
Greet(); // Hello, User
}
}

5. ref, out, params Keywords

5.1 ref

Passes variable by reference, requires initialization before calling.


void UpdateValue(ref int x)
{
x += 10;
}

int num = 5;
UpdateValue(ref num);
Console.WriteLine(num); // 15

5.2 out

Passes variable by reference, does not require initialization before calling.


void GetValues(out int a, out int b)
{
a = 10;
b = 20;
}

int x, y;
GetValues(out x, out y);
Console.WriteLine($"{x}, {y}"); // 10, 20

5.3 params

Allows variable number of arguments.


int Sum(params int[] numbers)
{
int total = 0;
foreach (int num in numbers)
total += num;
return total;
}

Console.WriteLine(Sum(1, 2, 3)); // 6
Console.WriteLine(Sum(5, 10, 15, 20));// 50

6. Recursion

A method can call itself; this is called recursion.


int Factorial(int n)
{
if (n == 0 || n == 1)
return 1;
return n * Factorial(n - 1);
}

Console.WriteLine(Factorial(5)); // 120

Explanation:

  1. Factorial(5) → 5 * 4 * 3 * 2 * 1 = 120
  2. Recursion must have a base case to avoid infinite calls.

Summary of Chapter 4:

  1. Methods improve code modularity and reusability.
  2. Methods can have parameters and return values.
  3. Overloading allows multiple methods with the same name.
  4. Optional parameters simplify method calls.
  5. ref, out, params provide advanced parameter handling.
  6. Recursion allows a method to call itself, useful for mathematical and algorithmic problems.