<aside> Methods are essential building blocks in C# that organize code into reusable, task-specific units. Think of them as specialized functions that can accept inputs (parameters) and return outputs.
</aside>
A method is a block of code that performs a specific task. Like a recipe, it takes ingredients (parameters) and produces a result (return value).
public static int CalculateSum(int num1, int num2)
{
return num1 + num2;
}
public void DisplayMessage(string message)
{
Console.WriteLine(message);
} // No return value
public int MultiplyNumbers(int x, int y)
{
return x * y;
} // Returns an integer
public static double CalculateArea(double radius)
{
return Math.PI * radius * radius;
} // Called without creating an instance
Modifier | Access Level |
---|---|
public | Accessible everywhere |
private | Only within same class |
protected | Class and derived classes |
internal | Same assembly only - Meaning the entire program can internally access this value but cannot be modified externally |
protected internal | Protected OR internal access |