Local functions (C# 6 note) & overloading
Overload methods safely (same name, different parameters) and, since C# 6 has no local functions, emulate them with small private helpers.
Overview
Goal: Reuse one method name for related work (overloading). In C# 6 there are no local functions; use tiny private helpers instead.
Overloading basics
Overloads share a name but differ by parameter types or count. The compiler picks the best match.
using System;
public static class MathX
{
public static int Sum(int a, int b) { return a + b; }
public static double Sum(double a, double b) { return a + b; }
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Sum ints = " + MathX.Sum(2, 3));
Console.WriteLine("Sum doubles = " + MathX.Sum(2.5, 3.1));
}
}
All lessons in this course
- Signatures, returns; expr-bodied; optional/named; ref/out; overloading
- Optional/named params; ref/out/in (basics)
- Local functions (C# 6 note) & overloading