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 theMain() 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
*(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 isvoid. If it returns an integer, the type is int, and it MUST contain a return statement.
csharp
5. Parameters and Arguments
Information can be passed *into* a method using parameters.
csharp
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
7. Optional and Named Parameters
You can give parameters default values. If the caller doesn't provide an argument, the default is used.
csharp
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 thereturn 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 saysstatic int Calculate(), the compiler will throw an error if you forget toreturnan 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, notcalculateTotal).
11. Exercises
-
1.
Write a method called
Multiplythat takes twointparameters and returns their product. Test it inMain().
-
2.
Create an overloaded version of
Multiplythat takes threeintparameters.
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?
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 (
refandoutkeywords).
14. Summary
Methods modularize code, making it readable, reusable, and testable. They accept inputs via parameters and return outputs via thereturn keyword. Features like Method Overloading and optional parameters make C# methods incredibly flexible.