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.
Local functions (C# 6 note) & overloading is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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));
}
}
Arity overloads
Changing the arity (number of parameters) creates a clear overload set.
using System;
public static class Printer
{
public static void Print(string text)
{
Console.WriteLine(text);
}
public static void Print(string text, int times)
{
for (int i = 0; i < times; i = i + 1)
{
Console.WriteLine(text);
}
}
}
public class Program
{
public static void Main(string[] args)
{
Printer.Print("Hello");
Printer.Print("Hi", 2);
}
}
Return type rule
Rule: Return type alone cannot differentiate overloads. Change parameter types, count, or modifiers (e.g., ref/out) for a valid overload.
Local-fn alternative
No local functions in C# 6: extract a small private method to keep the main method tidy.
using System;
// No local functions in C# 6: use a tiny private helper inside the class.
public class Report
{
private static string Header(string title)
{
return "-- " + title + " --";
}
public static void Print(string title, string body)
{
Console.WriteLine(Header(title));
Console.WriteLine(body);
}
}
public class Program
{
public static void Main(string[] args)
{
Report.Print("Status", "All green");
}
}
Overload resolution
The compiler picks the best match from the overload set. If calls are ambiguous, add a cast or adjust signatures.
using System;
public static class Formatter
{
public static string Format(int n) { return "N=" + n; }
public static string Format(string s) { return "S=" + s; }
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Formatter.Format(5)); // picks int overload
Console.WriteLine(Formatter.Format("hello")); // picks string overload
// Avoid ambiguity: cast when needed
Console.WriteLine(Formatter.Format((int)3));
}
}
Overloading rule
Recap
Recap: Build overload sets by changing parameter types or count, not return type. In C# 6, replace local functions with tiny private helpers to keep methods clean.
Frequently asked questions
Is the “Local functions (C# 6 note) & overloading” lesson free?
Yes — the full text of “Local functions (C# 6 note) & overloading” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Local functions (C# 6 note) & overloading” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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