C#.NET Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

C# Method and Function

Back to: C#.NET Tutorial

In C#, methods are blocks of code that perform a specific task. They help organize and reuse code, making it more readable and maintainable. Here’s an overview of methods in C#, including how to define and call them, as well as their types and some common use cases.

1. Defining a Method

A method is defined with a specific syntax that includes an access modifier, return type, method name, parameters (optional), and a body. Here’s the general structure:

[access_modifier] return_type MethodName(parameter_list)

{ // Method body // Code to execute }

Example:

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

2. Access Modifiers

Access modifiers control the visibility of a method. Common modifiers include:

  • public: Accessible from anywhere.
  • private: Accessible only within the same class.
  • protected: Accessible within the class and by derived class instances.
  • internal: Accessible within the same assembly.
  • protected internal: Accessible within the same assembly and derived classes.

3. Return Types

A method can return a value of a specified type, or it can return void if it doesn’t return anything.

Example of a method with a return type:

public string GetGreeting()
{
 return "Hello, World!";
}

Example of a void method:

public void PrintMessage(string message)
{
Console.WriteLine(message);
}

4. Calling a Method

You call a method by using its name and passing the required arguments (if any).

Example:

int sum = Add(3, 4);
// Calls the Add method and stores the result
Console.WriteLine(sum);
// Outputs: 7
PrintMessage("Welcome to C#!");
// Calls the PrintMessage method

5. Method Overloading

C# allows you to define multiple methods with the same name but different parameter lists (type, number, or both). This is known as method overloading.

Example:

public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; }

6. Static vs. Instance Methods

  • Static methods belong to the class itself rather than to any specific object. They can be called without creating an instance of the class.
  • Instance methods require an object of the class to be called.

Example of a static method:

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

7. Anonymous Methods and Lambda Expressions

C# supports anonymous methods and lambda expressions for defining methods without names, often used for delegates and LINQ queries.

Example of a lambda expression:

Func<int,> multiply = (x, y) => x * y;
Console.WriteLine(multiply(3, 4));
// Outputs: 12 </int,>

8. Asynchronous Methods

C# supports asynchronous programming with async and await, allowing methods to run without blocking the main thread.

Example:

public async Task FetchDataAsync()
{
await Task.Delay(1000); // Simulates an asynchronous operation return "Data fetched!";
}

Summary

  • Methods are defined with an access modifier, return type, name, parameters, and a body.
  • They can be overloaded, static, or instance-based.
  • C# supports advanced features like anonymous methods, lambda expressions, and asynchronous programming.

Using methods effectively can greatly enhance the structure and readability of your code

Scroll to Top