As your C# programs grow, you’ll quickly realise that repeating code is inefficient and error-prone. That’s where methods come in. Methods (also known as functions) allow you to group lines of code into reusable blocks that perform specific tasks.
In this post, you’ll learn how to define, call, and structure methods in C#. You’ll also learn how to pass values into them, get results back, and build cleaner, more modular programs.
🔹 What Is a Method?
A method is a named block of code that performs a specific task. Instead of copying and pasting the same logic multiple times, you write it once in a method and call it whenever needed.
Think of a method like a recipe: you give it some ingredients (parameters), it follows a process (body), and it gives you a result (return value).
✍️ Defining a Method in C#
🔸 Syntax
<access modifier> <return type> <method name>([parameters])
{
// Code block
}
✅ Example: A Method That Prints a Message
static void SayHello()
{
Console.WriteLine("Hello from a method!");
}
This method:
- Is
static
(meaning it’s called on the class, not an object) - Returns
void
(no value) - Takes no parameters
▶️ Calling a Method
Once defined, you call (execute) a method using its name and parentheses:
SayHello();
This tells the program to run the code inside SayHello()
.
🎁 Passing Parameters to Methods
Parameters let you pass information into a method.
static void Greet(string name)
{
Console.WriteLine("Hello, " + name + "!");
}
🧪 Call the method like this:
Greet("Alice");
Greet("Bob");
🔹 Tip: You can have multiple parameters, separated by commas.
static void PrintDetails(string name, int age)
{
Console.WriteLine($"{name} is {age} years old.");
}
🔄 Returning Values from Methods
If you want your method to give a result back, use a return type instead of void
.
static int Add(int a, int b)
{
return a + b;
}
Then use the result:
int sum = Add(5, 7);
Console.WriteLine("The sum is: " + sum);
🔁 Full Example: Putting It All Together
class Program
{
static void Main(string[] args)
{
SayHello();
Greet("Charlie");
int result = Multiply(3, 4);
Console.WriteLine("3 x 4 = " + result);
}
static void SayHello()
{
Console.WriteLine("Welcome to C# Methods!");
}
static void Greet(string name)
{
Console.WriteLine("Hi, " + name);
}
static int Multiply(int a, int b)
{
return a * b;
}
}
🤔 Why Use Methods?
✅ Cleaner Code: Break large programs into smaller, logical pieces
✅ Reusability: Avoid repeating the same code
✅ Maintainability: Easier to read and update
✅ Testing: Isolate functionality for debugging and unit testing
🧠 Method Naming Tips
- Use camelCase or PascalCase depending on convention
- Name methods based on what they do:
CalculateTax()
,DisplayMenu()
- Avoid vague names like
DoStuff()
orProcess1()
📚 Summary
Concept | Example |
---|---|
Define method | static void Greet(string name) |
Call method | Greet("Emma"); |
Return value | int result = Multiply(2, 3); |
Void method | static void PrintMessage() |
🚀Have a Question?
📬 Have a question about methods? Reach out to us via our Contact Page or join one of our live training bootcamps.