Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between Method and Function in C#
In C#, the terms method and function are often used interchangeably, but there is a subtle distinction. A method is a function that belongs to a class or struct, while a function is a more general term for a reusable block of code that performs a specific task.
Every C# program has at least one class with a method named Main. Methods are defined within classes and operate on the data and behavior of those classes.
Syntax
Following is the syntax for defining a method in C# −
[access modifier] [return type] MethodName(parameters) {
// method body
return value; // if return type is not void
}
Key Terminology
| Term | Definition | Usage in C# |
|---|---|---|
| Method | A function that belongs to a class or struct | Primary term used in C# documentation |
| Function | General term for reusable code block | Sometimes used informally for methods |
| Procedure | A method that returns void | Less commonly used term |
Using Methods in Classes
Methods encapsulate functionality within classes and can access class members. Here's a complete example −
using System;
class NumberManipulator {
public int FindMax(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
return result;
}
public void DisplayResult(int max) {
Console.WriteLine("The maximum value is: " + max);
}
}
class Program {
public static void Main() {
NumberManipulator nm = new NumberManipulator();
int result = nm.FindMax(15, 25);
nm.DisplayResult(result);
// Direct method call
int anotherResult = nm.FindMax(100, 200);
nm.DisplayResult(anotherResult);
}
}
The output of the above code is −
The maximum value is: 25 The maximum value is: 200
Static Methods vs Instance Methods
Methods can be either static (belong to the class) or instance methods (belong to object instances) −
using System;
class MathOperations {
// Static method - belongs to the class
public static int Add(int a, int b) {
return a + b;
}
// Instance method - belongs to object
public int Multiply(int a, int b) {
return a * b;
}
}
class Program {
public static void Main() {
// Static method call - no object needed
int sum = MathOperations.Add(10, 20);
Console.WriteLine("Sum: " + sum);
// Instance method call - object required
MathOperations math = new MathOperations();
int product = math.Multiply(5, 6);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Sum: 30 Product: 30
Method Overloading
C# supports method overloading, allowing multiple methods with the same name but different parameters −
using System;
class Calculator {
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;
}
}
class Program {
public static void Main() {
Calculator calc = new Calculator();
Console.WriteLine("Integer addition: " + calc.Add(5, 10));
Console.WriteLine("Double addition: " + calc.Add(3.5, 2.1));
Console.WriteLine("Three numbers: " + calc.Add(1, 2, 3));
}
}
The output of the above code is −
Integer addition: 15 Double addition: 5.6 Three numbers: 6
Conclusion
In C#, methods and functions refer to the same concept - reusable blocks of code that perform specific tasks. The term "method" is preferred in C# since these functions always belong to classes or structs. Methods can be static or instance-based and support features like overloading for enhanced functionality.
