Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Methods Explained: Declaration, Calling, Parameters, Return Types & More

C# Methods Explained

Master the fundamentals of C# methods—how to declare them, invoke them, handle parameters, return values, and leverage built‑in functionality—with clear examples.

A method is a reusable block of code that performs a specific task. For instance, if you’re building a drawing program that needs to create and color a circle, you could split the work into two distinct methods: one to draw the circle and another to color it. This modular approach improves readability, testability, and reusability.


Declaring a Method in C#

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

returnType methodName() {
  // method body
}

Components:

Example:

void display() {
  // code
}

Here, display() is the method name and void indicates it does not return a value.


Calling a Method in C#

Once declared, you invoke a method by its name followed by parentheses. For the display() example:

// call the method
display();
C# Methods Explained: Declaration, Calling, Parameters, Return Types & More

Practical Example: Using a Method

using System;

namespace MethodDemo {
  class Program {
    // Method declaration
    public void Display() {
      Console.WriteLine("Hello World");
    }

    static void Main(string[] args) {
      // Create an instance of Program
      Program p1 = new Program();

      // Invoke the Display method
      p1.Display();

      Console.ReadLine();
    }
  }
}

Output

Hello World

This demonstrates creating an object p1 of the Program class and calling its Display() method.


Method Return Types

Methods can return data or be void. When a return value is required, the return statement sends the value back to the caller, and the method’s return type must match the value’s type.

int AddNumbers() {
  int sum = 5 + 14;
  return sum;
}

Example: Returning an Integer

using System;

namespace MethodDemo {
  class Program {
    // Static method with return type
    static int AddNumbers() {
      int sum = 5 + 14;
      return sum;
    }

    static void Main(string[] args) {
      int sum = AddNumbers();
      Console.WriteLine(sum);
      Console.ReadLine();
    }
  }
}

Output

19

Note that static methods belong to the class itself, so no instance is required to call them.


Methods with Parameters

Parameters allow methods to accept input values. They are declared in the method signature and passed during invocation.

int AddNumber(int a, int b) {
  return a + b;
}

Calling the method with arguments:

AddNumber(100, 100);
C# Methods Explained: Declaration, Calling, Parameters, Return Types & More

Example 1: Adding Two Numbers

using System;

namespace MethodDemo {
  class Program {
    int AddNumber(int a, int b) {
      return a + b;
    }

    static void Main(string[] args) {
      Program p1 = new Program();
      int sum = p1.AddNumber(100, 100);
      Console.WriteLine("Sum: " + sum);
      Console.ReadLine();
    }
  }
}

Output

Sum: 200

Example 2: Single Parameter Method

using System;

namespace MethodDemo {
  class Program {
    string Work(string task) {
      return task;
    }

    static void Main(string[] args) {
      Program p1 = new Program();
      string result = p1.Work("Cleaning");
      Console.WriteLine("Work: " + result);
      Console.ReadLine();
    }
  }
}

Output

Work: Cleaning

Built‑In Methods

Beyond user‑defined methods, C# offers a rich library of built‑in methods. For example:

Example: Using Math.Sqrt

using System;

namespace MethodDemo {
  class Program {
    static void Main(string[] args) {
      double result = Math.Sqrt(9);
      Console.WriteLine("Square root of 9: " + result);
    }
  }
}

Output

Square root of 9: 3

Here, Math.Sqrt is part of the System.Math class in .NET, documented by Microsoft.

Learn more about C# built‑in methods.


Method Overloading

C# allows multiple methods with the same name, provided their parameter lists differ. This is called method overloading.

using System;

namespace MethodOverloadDemo {
  class Program {
    // One-parameter overload
    void Display(int a) {
      Console.WriteLine("Arguments: " + a);
    }

    // Two-parameter overload
    void Display(int a, int b) {
      Console.WriteLine("Arguments: " + a + " and " + b);
    }

    static void Main(string[] args) {
      Program p1 = new Program();
      p1.Display(100);
      p1.Display(100, 200);
      Console.ReadLine();
    }
  }
}

Output

Arguments: 100
Arguments: 100 and 200

Overloading enhances readability and flexibility. For deeper insights, see C# Method Overloading.



C Language

  1. Branch Current Method: A Step‑by‑Step Guide to Solving Circuit Networks
  2. Mastering the Node Voltage Method for Precise Circuit Analysis
  3. Mastering C# Abstract Classes & Methods: A Practical Guide
  4. Understanding C# Partial Classes and Methods
  5. Master sealed classes and methods in C#—learn when to use them, see clear examples, and boost code safety and performance.
  6. Mastering Method Overloading in C#: Concepts, Examples, and Best Practices
  7. Java Methods: How to Define, Call, and Use Them Effectively
  8. Mastering Java Polymorphism: Concepts, Examples, and Best Practices
  9. C# Methods: Defining, Calling, and Using Functions
  10. EPA Method 21: Comprehensive Leak Detection & Repair Protocol