Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 11 Beginner

Methods and Parameters in C#

Updated: May 17, 2026
5 min read

# CHAPTER 11

Methods and Parameters

1. Introduction

Writing a 2,000-line program entirely inside the Main() method is a nightmare to read and debug. Methods (also known as Functions) allow you to break your code into small, reusable blocks. A method takes inputs, performs a specific task, and returns a result.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define and call custom methods.
  • Understand Return Types (void, int, string, etc.).
  • Pass data using Parameters.
  • Implement Method Overloading.
  • Use Optional and Named Parameters.

3. Declaring and Calling a Method

A method must be declared inside a class.

Syntax: AccessModifier ReturnType MethodName(Parameters) { // body }

csharp
1234567891011121314151617
using System;

class Program
{
    // Method Definition
    static void SayHello() 
    {
        Console.WriteLine("Hello from a custom method!");
    }

    static void Main(string[] args)
    {
        // Method Call
        SayHello(); 
        SayHello(); // Can be reused!
    }
}

*(Note: We use static here because Main is static, and static methods can only directly call other static methods).*

4. Return Types

A method can perform a calculation and hand the result *back* to the code that called it. If a method does not return anything, its return type is void. If it returns an integer, the type is int, and it MUST contain a return statement.
csharp
1234567891011121314
class Program
{
    // Returns an integer
    static int GetLuckyNumber() 
    {
        return 7;
    }

    static void Main()
    {
        int myNum = GetLuckyNumber();
        Console.WriteLine(myNum); // Prints 7
    }
}

5. Parameters and Arguments

Information can be passed *into* a method using parameters.
csharp
12345678910111213
class Program
{
    // Takes two parameters: string and int
    static void GreetUser(string name, int age) 
    {
        Console.WriteLine($"Hello {name}, you are {age} years old.");
    }

    static void Main()
    {
        GreetUser("Alice", 25); // "Alice" and 25 are the Arguments
    }
}

6. Method Overloading

In C#, you can create multiple methods with the exact same name, as long as they have different parameters (either different types or a different number of parameters).
csharp
12345678910111213141516171819
class Program
{
    static int Add(int a, int b) 
    {
        return a + b;
    }

    // Overloaded Method
    static double Add(double a, double b) 
    {
        return a + b;
    }

    static void Main()
    {
        Console.WriteLine(Add(5, 5));       // Calls the 'int' version
        Console.WriteLine(Add(2.5, 3.5));   // Calls the 'double' version
    }
}

7. Optional and Named Parameters

You can give parameters default values. If the caller doesn't provide an argument, the default is used.
csharp
1234567891011121314151617
class Program
{
    // 'country' is an optional parameter
    static void Register(string name, string country = "USA") 
    {
        Console.WriteLine($"{name} is from {country}");
    }

    static void Main()
    {
        Register("John");           // "John is from USA"
        Register("Maria", "Spain"); // "Maria is from Spain"
        
        // Named Parameters (C# feature for clarity)
        Register(country: "Japan", name: "Kenji"); 
    }
}

8. OOP and Memory Explanation

When a method is called, the CLR creates a Stack Frame in the Stack memory. All the local variables and parameters for that method are stored in this frame. When the method hits the return statement, the Stack Frame is immediately destroyed, and all local variables are erased from memory. This is why a variable declared inside MethodA cannot be seen by MethodB (Variable Scope).

9. Common Mistakes

  • Forgetting return: If your method signature says static int Calculate(), the compiler will throw an error if you forget to return an integer.
  • Confusing Parameters with Arguments: Parameters are the variables in the method definition (int x). Arguments are the actual values passed during the call (5).

10. Best Practices

  • Single Responsibility Principle: A method should do exactly ONE thing. If a method is 100 lines long and formats data, saves to a database, and sends an email, break it down into three separate methods.
  • PascalCase Naming: In C#, method names should always start with a capital letter (CalculateTotal, not calculateTotal).

11. Exercises

  1. 1. Write a method called Multiply that takes two int parameters and returns their product. Test it in Main().
  1. 2. Create an overloaded version of Multiply that takes three int parameters.

12. MCQs with Answers

Question 1

What keyword is used as a return type when a method returns nothing?

Question 2

What keyword sends data back from a method to the caller?

Question 3

Variables declared inside a method definition (e.g., MethodName(int x)) are called:

Question 4

Actual values passed to a method during a call (e.g., MethodName(5)) are called:

Question 5

Can two methods have the same name in the same class?

Question 6

What makes a parameter optional?

Question 7

What happens in memory when a method finishes executing?

Question 8

What C# naming convention is standard for Methods?

Q9. Can a static method directly call a non-static method in the same class? a) Yes b) No, static methods can only directly call other static methods Answer: b) No
Question 10

What is a Named Argument?

13. Interview Questions

  • Q: Explain Method Overloading. Is changing the return type enough to overload a method? (Answer: No, the parameter list must differ).
  • Q: Explain the difference between Passing by Value and Passing by Reference (ref and out keywords).

14. Summary

Methods modularize code, making it readable, reusable, and testable. They accept inputs via parameters and return outputs via the return keyword. Features like Method Overloading and optional parameters make C# methods incredibly flexible.

15. Next Chapter Recommendation

You now know the procedural basics of C#. However, C# is an Object-Oriented language. In Chapter 12: Object-Oriented Programming Basics, we will shift our mindset and learn the 4 pillars of modern software design.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·