In this article we are going to discuss about method with examples in C#.
What is a Method in C#?
A method in C# is a block of code that contains a series of statements. Methods are used to perform operations, and they promote code reusability and modularity. Here is an example of a simple method.
Example
In this example, the add method takes two integers as parameters and returns their sum.
public class Calculator
{
// Method to add two numbers
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
int result = calc.Add(3, 4);
Console.WriteLine(result); // Output: 7
}
}