CHASH Tutorial



C# METHODS


C# Methods

A method in C# is a block of code that performs a particular task. Methods help you reuse code, keep it organized, and make it easier to read and maintain.

Why use methods?
Methods reduce repetition, divide complex programs into small parts, and increase reusability.

Basic Method Syntax

returnType MethodName(parameters)
{
    // code to execute
}
  

Example: Simple Method

using System;

class Program
{
    static void Greet()
    {
        Console.WriteLine("Hello, welcome to C#!");
    }

    static void Main()
    {
        Greet(); // Call the method
    }
}
  

Method with Parameters

static void PrintMessage(string name)
{
    Console.WriteLine("Hello " + name);
}

static void Main()
{
    PrintMessage("Technorank");
}
  

Method with Return Value

static int Add(int a, int b)
{
    return a + b;
}

static void Main()
{
    int result = Add(5, 7);
    Console.WriteLine(result);  // Output: 12
}
  

Method Overloading

You can define multiple methods with the same name but different parameters.

static int Multiply(int a, int b)
{
    return a * b;
}

static double Multiply(double a, double b)
{
    return a * b;
}

static void Main()
{
    Console.WriteLine(Multiply(4, 3));       // 12
    Console.WriteLine(Multiply(2.5, 4.2));   // 10.5
}
  
📌 Note: Methods can be static (used without creating an object) or instance-level (called via objects).

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review