Methods and Parameter Usage in C#

Published: (February 2, 2026 at 01:33 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Defining and Calling Methods

static void Greet()
{
    Console.WriteLine("Hello!");
}

static void Main()
{
    Greet(); // method call
}
// Output:
Hello!

Methods with Parameters

static void Greet(string name)
{
    Console.WriteLine("Hello " + name + "!");
}

static void Main()
{
    Greet("Alice");
    Greet("Bob");
}
// Output:
Hello Alice!
Hello Bob!

Default Parameters

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

static void Square(int number) // parameter: number
{
    Console.WriteLine(number * number);
}

static void Main()
{
    Square(4); // argument: 4
}
// Output:
16

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

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
}
// Output:
120

ref and out Parameters

// 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
}
// Output:
6
42

Multiple Parameters with params

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
}
// Output:
6
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.
}
// Output:
C# is a powerful language.

Local Functions

static void Main()
{
    int Multiply(int a, int b) // local function
    {
        return a * b;
    }

    Console.WriteLine(Multiply(3, 4)); // 12
}
// Output:
12

  • Methods are reusable blocks of code.
  • Parameters are used to pass data into methods.
  • return is used to return a value.
  • Default parameters make code flexible.
  • Overloading allows defining methods with the same name but different parameters.
  • ref and out parameters allow modifying values by reference.
  • params enables variable numbers of parameters.
  • Local functions are helper methods defined inside another method.
  • Recursion means a method calling itself to solve a problem.
Back to Blog

Related posts

Read more »

C# 14 extension blocks

Introduction Learn about C 14 extension blockshttps://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methodsdeclare-ex...