codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

13 / 60 topics
13Methods in C#14Parameters and Arguments15Return Values from Methods16Recursion in C#
Tutorials/C# Programming/Methods in C#
🔷C# Programming

Methods in C#

Updated 2026-05-15
10 min read

Methods in C#

Introduction

In the world of programming, methods are blocks of code that perform specific tasks. They allow you to organize your code into manageable and reusable pieces. In this tutorial, we will explore how to define and call methods in C#. Understanding methods is fundamental for writing efficient and maintainable code.

Concept

Methods in C# are similar to functions in other programming languages. They consist of a method signature and a method body. The method signature includes the return type, method name, and parameters, while the method body contains the statements that define what the method does.

Key Components of a Method

  1. Return Type: Specifies the data type of the value returned by the method. If the method doesn't return any value, it is denoted by void.
  2. Method Name: A unique identifier for the method.
  3. Parameters: Input values passed to the method when it is called.

Method Syntax

The basic syntax for defining a method in C# is as follows:

<returnType> <methodName>([parameterList])
{
    // Method body
}

- `<returnType>`: The data type of the value returned by the method.
- `<methodName>`: The name of the method.
- `[parameterList]`: A list of parameters, each with a data type and name.

## Examples

Let's dive into some practical examples to understand how methods work in C#.

### Example 1: Simple Method without Parameters

Here is an example of a simple method that prints "Hello, World!" to the console:

```csharp
using System;

class Program
{
    static void Main(string[] args)
    {
        // Call the PrintMessage method
        PrintMessage();
    }

    // Define the PrintMessage method
    static void PrintMessage()
    {
        Console.WriteLine("Hello, World!");
    }
}

In this example:
- The `PrintMessage` method is defined with a return type of `void`, meaning it doesn't return any value.
- The method body contains a single statement that prints "Hello, World!" to the console.
- The `Main` method calls the `PrintMessage` method.

### Example 2: Method with Parameters

Now, let's look at an example where we pass parameters to a method:

```csharp
using System;

class Program
{
    static void Main(string[] args)
    {
        // Call the AddNumbers method with arguments
        int result = AddNumbers(5, 3);
        Console.WriteLine("The sum is: " + result);
    }

    // Define the AddNumbers method with parameters
    static int AddNumbers(int a, int b)
    {
        return a + b;
    }
}

In this example:
- The `AddNumbers` method takes two integer parameters `a` and `b`.
- It returns the sum of these two numbers.
- The `Main` method calls `AddNumbers` with arguments `5` and `3`, and stores the result in a variable.

### Example 3: Method with Return Type

Here is an example where a method returns a value:

```csharp
using System;

class Program
{
    static void Main(string[] args)
    {
        // Call the GetGreeting method
        string greeting = GetGreeting("Alice");
        Console.WriteLine(greeting);
    }

    // Define the GetGreeting method with a return type of string
    static string GetGreeting(string name)
    {
        return "Hello, " + name + "!";
    }
}

In this example:

  • The GetGreeting method takes a single string parameter name.
  • It returns a greeting message that includes the provided name.
  • The Main method calls GetGreeting with the argument "Alice" and prints the returned value.

What's Next?

Now that you have learned how to define and call methods in C#, it's time to explore more advanced topics such as parameters and arguments. Understanding these concepts will help you create more flexible and powerful methods in your applications.

Stay tuned for the next tutorial where we will dive deeper into parameters, different types of parameters, and how they can be used effectively in your code.


PreviousBreak and Continue StatementsNext Parameters and Arguments

Recommended Gear

Break and Continue StatementsParameters and Arguments