C# — Methods
Basic methods
using System;
class Calculator
{
// Method with return value
public int Add(int a, int b)
{
return a + b;
}
// Expression-bodied method
public int Multiply(int a, int b) => a * b;
// Void method
public void PrintSum(int a, int b)
{
Console.WriteLine($"{a} + {b} = {a + b}");
}
static void Main()
{
var calc = new Calculator();
Console.WriteLine(calc.Add(3, 4)); // 7
Console.WriteLine(calc.Multiply(3, 4)); // 12
calc.PrintSum(3, 4); // 3 + 4 = 7
}
}
Parameters
using System;
class Examples
{
// Value parameters
static void ByValue(int x)
{
x = 100; // Only changes local copy
}
// Reference parameters
static void ByReference(ref int x)
{
x = 100; // Changes the original
}
// Out parameters
static void Divide(int a, int b, out int quotient, out int remainder)
{
quotient = a / b;
remainder = a % b;
}
// Default parameters
static void Greet(string name, string greeting = "Hello")
{
Console.WriteLine($"{greeting}, {name}!");
}
// Named arguments
static void CreateProfile(string name, int age, string city = "Unknown")
{
Console.WriteLine($"{name}, {age}, {city}");
}
static void Main()
{
int a = 5;
ByValue(a);
Console.WriteLine($"After ByValue: {a}"); // 5
ByReference(ref a);
Console.WriteLine($"After ByReference: {a}"); // 100
Divide(10, 3, out int q, out int r);
Console.WriteLine($"10 / 3 = {q} remainder {r}");
Greet("Alice"); // Hello, Alice!
Greet("Bob", "Hi"); // Hi, Bob!
// Named arguments
CreateProfile(age: 30, name: "Alice", city: "NYC");
}
}
Params
using System;
class Stats
{
static int Sum(params int[] numbers)
{
int total = 0;
foreach (int n in numbers)
total += n;
return total;
}
static void Main()
{
Console.WriteLine(Sum(1, 2, 3)); // 6
Console.WriteLine(Sum(1, 2, 3, 4, 5)); // 15
}
}
Method overloading
using System;
class Printer
{
static void Print(int value) => Console.WriteLine($"Int: {value}");
static void Print(double value) => Console.WriteLine($"Double: {value}");
static void Print(string value) => Console.WriteLine($"String: {value}");
static void Print(int[] values) =>
Console.WriteLine($"Array: [{string.Join(", ", values)}]");
static void Main()
{
Print(42); // Int: 42
Print(3.14); // Double: 3.14
Print("hello"); // String: hello
Print(new[] { 1, 2, 3 }); // Array: [1, 2, 3]
}
}
Local functions
using System;
class Program
{
static void Main()
{
// Local function inside Main
int Factorial(int n)
{
if (n <= 1) return 1;
return n * Factorial(n - 1);
}
Console.WriteLine($"5! = {Factorial(5)}");
// Lambda-style local function
int Square(int x) => x * x;
Console.WriteLine($"5² = {Square(5)}");
}
}
Static methods
using System;
class MathHelper
{
// Static method — called on the class, not an instance
public static double CircleArea(double radius)
{
return Math.PI * radius * radius;
}
public static int Max(int a, int b) => a > b ? a : b;
}
class Program
{
static void Main()
{
Console.WriteLine(MathHelper.CircleArea(5)); // 78.5398
Console.WriteLine(MathHelper.Max(3, 7)); // 7
}
}
Async methods
using System;
using System.Threading.Tasks;
class DataFetcher
{
static async Task<string> FetchDataAsync(string url)
{
// Simulate async work
await Task.Delay(1000);
return $"Data from {url}";
}
static async Task Main()
{
Console.WriteLine("Fetching...");
string result = await FetchDataAsync("https://api.example.com");
Console.WriteLine(result);
}
}
Delegates
using System;
// Define delegate type
delegate int MathOperation(int a, int b);
class Program
{
static int Add(int a, int b) => a + b;
static int Multiply(int a, int b) => a * b;
static void Main()
{
MathOperation op = Add;
Console.WriteLine(op(3, 4)); // 7
op = Multiply;
Console.WriteLine(op(3, 4)); // 12
// Using Func and Action
Func<int, int, int> addFunc = (a, b) => a + b;
Action<string> printAction = s => Console.WriteLine(s);
Console.WriteLine(addFunc(5, 3));
printAction("Hello!");
}
}
Mini Practice
Write C# code that:
- Creates methods with
refandoutparameters - Demonstrates method overloading
- Uses a local function for recursion
- Creates an async method
Up Next
In the next lesson, you'll learn about Classes — object-oriented programming in C#.
Related Topics
Frequently Asked Questions about Methods
What is Methods in C#?
Methods is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Methods?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Methods.
Why is Methods important in C#?
Methods is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.