Loading...

Methods in C#

Imagine you run a small shop. Every day, customers ask the same questions:

“What’s the price?”
“Do you have a discount?”
“Can you total my bill?”

Instead of repeating yourself thousands of times, you would create a single button that answers these questions automatically.

In programming, that “button” is called a method.

A method lets you write code once and reuse it again and again, saving time, reducing mistakes, and making your programs cleaner and smarter.

Today, you’ll learn how to create your own reusable tools inside C#.

The Project: Math Utilities Library

You’ll build a small toolset containing:

  • A method to add two numbers
  • A method to subtract
  • A method to multiply
  • A method to find the square of a number

Your program will use these methods to perform calculations, just like a tiny calculator engine.

By the end, you’ll have your own Math Utilities Library!

What Is a Method?

A method is a named block of code that performs a specific task.

Methods help you:

  • Avoid repeating code
  • Organize logic into clear steps
  • Build reusable tools
  • Make programs easier to read

Example:

csharp
4 lines
|
11/ 500 tokens
1
2
3
4
int Add(int a, int b)
{
    return a + b;
}
Code Tools

This method takes two numbers and returns their sum.

Declaring and Calling Methods

A basic method looks like this:

csharp
4 lines
|
13/ 500 tokens
1
2
3
4
returnType MethodName(parameters)
{
    // code
}
Code Tools

To use it, just call it:

csharp
1 lines
|
6/ 500 tokens
1
int result = Add(5, 3);
Code Tools

This runs the Add method and stores the result.

Parameters (Inputs to a Method)

Parameters are placeholders for values you want to pass:

csharp
4 lines
|
12/ 500 tokens
1
2
3
4
int Multiply(int x, int y)
{
    return x * y;
}
Code Tools

Calling:

csharp
1 lines
|
4/ 500 tokens
1
Multiply(4, 6);
Code Tools

Here, x = 4 and y = 6.

Return Values

A method can return a value using return.

Example:

csharp
4 lines
|
13/ 500 tokens
1
2
3
4
double Square(double num)
{
    return num * num;
}
Code Tools

Some methods don’t return anything, they use void.

Variable Scope

Scope means where a variable can be used.

  • Variables inside a method exist only inside that method
  • They cannot be accessed outside
  • This keeps your code clean and prevents conflicts

Example:

csharp
4 lines
|
13/ 500 tokens
1
2
3
4
void Test()
{
    int x = 5; // x exists only here
}
Code Tools

Trying to use x outside the method will cause an error.

Let’s Build the Math Utilities Library

csharp
41 lines
|
195/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;

class Program
{
    // Method to add two numbers
    static int Add(int a, int b)
    {
        return a + b;
    }

    // Method to subtract
    static int Subtract(int a, int b)
    {
        return a - b;
    }

    // Method to multiply
    static int Multiply(int a, int b)
    {
        return a * b;
    }

    // Method to find the square
    static int Square(int n)
    {
        return n * n;
    }

    static void Main()
    {
        Console.WriteLine("Math Utilities Library");

        int x = 8;
        int y = 3;

        Console.WriteLine($"Add: {Add(x, y)}");
        Console.WriteLine($"Subtract: {Subtract(x, y)}");
        Console.WriteLine($"Multiply: {Multiply(x, y)}");
        Console.WriteLine($"Square of {x}: {Square(x)}");
    }
}
Code Tools

This simple program demonstrates four reusable tools, your own math toolbox!

How It Works

  • Each method performs a single, clear task
  • Main() calls the methods with actual values
  • The results are printed neatly
  • You can reuse these methods anywhere in your program

Try modifying the numbers in the DevsCall AI Online Runner to see different outputs instantly.

Learn Together with AI

Try asking your AI Copilot:

  • “Add a Divide method with error checking.”
  • “Rewrite the Math Library to take user input.”
  • “Explain method scope in the simplest way.”
  • “Create a version where all methods return double instead of int.”

AI will help you explore different method designs.

Practice Time

Create three methods:

  1. int Max(int a, int b) → returns the bigger number
  2. bool IsEven(int n) → returns true if even
  3. double Half(double n) → returns n divided by 2

Then call all of them from Main() and print the results.

Expected sample output:

text
3 lines
|
8/ 500 tokens
1
2
3
Max: 10
IsEven: True
Half: 7.5

Example Solution

csharp
26 lines
|
100/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;

class Program
{
    static int Max(int a, int b)
    {
        return (a > b) ? a : b;
    }

    static bool IsEven(int n)
    {
        return n % 2 == 0;
    }

    static double Half(double n)
    {
        return n / 2.0;
    }

    static void Main()
    {
        Console.WriteLine(Max(10, 7));
        Console.WriteLine(IsEven(8));
        Console.WriteLine(Half(15));
    }
}
Code Tools

If this matches your output, you’re now writing clean, reusable C# code!

Frequently Asked Questions

A method is a named block of code that performs a specific task. It allows you to reuse logic without rewriting it.

Methods keep code organized, reduce repetition, improve readability, and help structure programs into smaller, manageable parts.

Parameters are inputs you pass into a method so it can work with different values each time it's called.

A return value is the result a method sends back after completing its task. Methods that don’t return anything use void.

Yes. Methods can call each other, which helps create clean and modular code structures.

No. Methods can have no parameters, one parameter, or many.

Still have questions?Contact our support team