Cross_Column

Wednesday, 1 July 2026

Methods & Methods Parameter in C#



Methods (Functions) & Method Parameters in C# | Beginner Tutorial

Methods (Functions) & Method Parameters in C#

Learn how to create reusable methods, pass parameters, return values, and write clean C# code with practical examples.

Table of Contents

  1. What is a Method?
  2. Why Use Methods?
  3. Method Syntax
  4. Method Parameters
  5. Return Values
  6. Optional Parameters
  7. Named Arguments
  8. ref, out and params
  9. Best Practices
  10. Summary

What is a Method?

A method (also called a function) is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you can write it once inside a method and call it whenever needed.

Note: Using methods makes your code cleaner, easier to understand, and easier to maintain.

Why Use Methods?

  • Reduce duplicate code.
  • Improve code readability.
  • Make applications easier to maintain.
  • Break large problems into smaller tasks.

Method Syntax

accessModifier returnType MethodName(parameters)
{
    // Code
}

Creating and Calling a Method

using System;

class Program
{
    static void Welcome()
    {
        Console.WriteLine("Welcome to C# Tutorials");
    }

    static void Main()
    {
        Welcome();
    }
}

Method Parameters

Parameters allow you to pass data into a method.

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

Greet("John");
Greet("David");

Methods with Return Values

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

int result = Add(10,20);

Console.WriteLine(result);
Tip: Use the return keyword whenever your method needs to send a value back to the caller.

Optional Parameters

static void Welcome(string name = "Guest")
{
    Console.WriteLine("Welcome " + name);
}

Welcome();
Welcome("John");

Named Arguments

static void Student(string name,int age)
{
    Console.WriteLine(name + " " + age);
}

Student(age:25,name:"John");

Using ref Parameter

static void Increase(ref int number)
{
    number++;
}

Using out Parameter

static void GetValues(out int x)
{
    x = 100;
}

Using params

static int Sum(params int[] numbers)
{
    int total = 0;

    foreach(int n in numbers)
        total += n;

    return total;
}

Console.WriteLine(Sum(10,20,30));
Common Mistake: Forgetting to use the correct return type or not returning a value from a non-void method causes compilation errors.

Complete Example

using System;

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

    static void Main()
    {
        int result = Multiply(5,6);

        Console.WriteLine(result);
    }
}

Best Practices

  • Keep methods small and focused.
  • Use meaningful method names.
  • Avoid methods that perform too many tasks.
  • Return values instead of using global variables.
  • Use parameters to make methods reusable.

Summary

ConceptDescription
MethodReusable block of code.
ParameterInput passed into a method.
ReturnReturns a value from a method.
voidNo value is returned.
paramsAccepts multiple arguments.
refPasses value by reference.
outReturns values through parameters.

Interview Questions

  1. What is a method in C#?
  2. What is the difference between parameters and arguments?
  3. What is the difference between void and return?
  4. When should you use ref and out?
  5. What is the purpose of params?

Practice Exercise

  • Create a method to print your name.
  • Create a method that adds two numbers.
  • Create a method that returns the largest of two numbers.
  • Create a method using optional parameters.
  • Create a method using params.

FAQ

Can a method return multiple values?
Yes. You can use out, tuples, or custom objects.

What is the difference between a parameter and an argument?
A parameter is defined in the method, while an argument is the value passed when calling the method.


Next Tutorial: Method Overloading in C#

No comments:

Post a Comment

Few More

Recursion in C#

Recursion in C# | Beginner Tutorial Recursion in C# Learn how recursion works in C#, understand the base case and re...