Introduction to Methods - Reusing Logic the Right Way
Vaibhav • September 9, 2025
In programming, repetition is unavoidable. Whether you are checking conditions, running loops, or displaying information, you will often need the same logic in multiple places. Copy-pasting the same block of code again and again seems like a quick fix, but it leads to clutter, mistakes, and a maintenance nightmare. That’s where methods come in. They allow you to bundle logic into reusable blocks and call them whenever needed.
What is a method?
A method is simply a named block of code that performs a specific task. You define it once, and then you can “call” it whenever you need that task done. This avoids repeating code and makes your program more organized and easier to maintain.
// A simple method that prints a greeting
void SayHello()
{
Console.WriteLine("Hello, world!");
}
// calling the method
SayHello(); // Output: Hello, world!
In the above example, SayHello
is a method. Whenever you call
SayHello()
, the code inside its body executes. Think of it as a way
of “teaching” your program a new word - after defining it once, you can use that word anywhere.
Why methods matter
- Reusability: Write code once and use it in multiple places.
- Readability: Give chunks of logic meaningful names instead of scattering raw code everywhere.
- Maintainability: Fix or update a piece of logic in one place, and it applies everywhere the method is used.
- Testing: Smaller, isolated methods are easier to test and debug than large, tangled code blocks.
In C#, methods must be declared inside a class. Since we haven’t formally introduced classes yet, we’ll keep examples simple and focus on the concept of methods. Later, we’ll revisit how methods live inside classes and interact with objects.
Everyday analogy
Think of a method like a recipe. Once you have the recipe written down, you don’t need to rewrite the entire instructions every time you cook that dish. Instead, you just say “follow the recipe” - that’s the method call.
Simple example with repetition
Imagine you want to display a welcome banner in multiple places in your program. Without methods, you’d
write the same Console.WriteLine
calls every time.
// Without methods - repetitive code
Console.WriteLine("************************");
Console.WriteLine(" Welcome to My App! ");
Console.WriteLine("************************");
Console.WriteLine("************************");
Console.WriteLine(" Welcome to My App! ");
Console.WriteLine("************************");
This works, but it’s wasteful and messy. If you ever want to change the message (e.g., “Welcome to My Store!”), you must update it in every single place. Instead, let’s put the banner into a method.
// Defining a method for the banner
void ShowBanner()
{
Console.WriteLine("************************");
Console.WriteLine(" Welcome to My App! ");
Console.WriteLine("************************");
}
// Using the method
ShowBanner();
ShowBanner();
Now the banner prints wherever you call ShowBanner()
, and if you
want to update the text, you only change it inside the method.
Key parts of a method
- Name: How you call the method (e.g.,
ShowBanner
). - Body: The code inside the curly braces
{ }
that runs when called. - Call: The act of “using” the method by writing its name followed by parentheses.
Methods are sometimes also called “functions” in other programming languages. In C#, the terms aren’t exactly the same, but at this stage, you can think of them as very similar concepts.
Best practices to start with
- Give methods descriptive names (
CalculateTotal
is better thanDoStuff
). - Keep them focused on one task - don’t try to do too much inside a single method.
- Use methods to reduce repetition and group related logic.
A good rule of thumb: if you find yourself repeating three or more lines of code in multiple places, that’s a strong signal you should wrap it in a method.
Summary
Methods are the building blocks of structured programs. They let you avoid repetition, organize your logic, and make your code easier to read, maintain, and test. In the upcoming articles, we’ll go deeper into how to declare methods properly, work with inputs (parameters), and return results.