delegate, Action/Func, closures
Pass behavior via delegates: declare custom delegates, use built-in Action/Func, and see how lambdas capture variables (closures).
delegate, Action/Func, closures is a free C# Academy lesson on CoddyKit — lesson 1 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.
Delegates overview
Goal: Treat methods as data.
- Define a delegate type
- Use Action/Func
- Create lambdas
- Understand closures (captured variables)
Custom delegate basics
A delegate is a type-safe function pointer. Assign compatible methods and invoke like a function.
using System;
// Custom delegate type: a method taking two ints and returning int
public delegate int Op(int x, int y);
public class Program
{
static int Add(int a, int b) { return a + b; }
static int Mul(int a, int b) { return a * b; }
public static void Main(string[] args)
{
Op f = Add; // method group conversion
Console.WriteLine(f(2, 3)); // 5
f = Mul;
Console.WriteLine(f(2, 3)); // 6
}
}
Action & Func
Action represents void-returning methods; Func returns a value. Use method groups or lambdas.
using System;
public class Program
{
static void Log(string s) { Console.WriteLine("LOG: " + s); }
public static void Main(string[] args)
{
Action<string> a = Log; // no return
a("hello");
Func<int, int, int> sum = delegate(int x, int y) { return x + y; }; // lambda/anon method
Console.WriteLine(sum(3, 4)); // 7
// Method group to Func
Func<string, int> len = GetLen;
Console.WriteLine(len("abc")); // 3
}
static int GetLen(string s) { return s == null ? 0 : s.Length; }
}
Closure demo
A closure captures variables from the outer scope. The variable outlives the method that created the delegate.
using System;
public class Program
{
public static void Main(string[] args)
{
int counter = 0; // captured variable (lives as long as delegate)
Action inc = delegate() { counter = counter + 1; Console.WriteLine(counter); };
inc(); // 1
inc(); // 2
Console.WriteLine("Final=" + counter); // 2
}
}
Higher-order method
Pass a Func or Action to customize behavior. This is the basis for many APIs (and LINQ).
using System;
public class Program
{
// Apply a transform to each number and sum the results
static int SumBy(int[] xs, Func<int, int> transform)
{
int total = 0;
for (int i = 0; i < xs.Length; i++)
{
total += transform(xs[i]);
}
return total;
}
public static void Main(string[] args)
{
int[] data = new int[] { 1, 2, 3 };
int sumSquares = SumBy(data, delegate(int x) { return x * x; });
Console.WriteLine(sumSquares); // 14
}
}
Delegate tips
Tips:
- Prefer Action/Func to avoid extra custom delegate types.
- Keep lambdas short and clear.
- Be mindful of captured variables; their value can change later.
- Use method groups when a named method already matches the signature.
Func vs Action meaning
Recap
Recap: Declare delegates, use Action/Func, and write small lambdas. Closures let lambdas capture local variables—use carefully.
Frequently asked questions
Is the “delegate, Action/Func, closures” lesson free?
Yes — the full text of “delegate, Action/Func, closures” 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 “delegate, Action/Func, closures”?
Pass behavior via delegates: declare custom delegates, use built-in Action/Func, and see how lambdas capture variables (closures). 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 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “delegate, Action/Func, closures” 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
- delegate, Action/Func, closures
- Events: publish/subscribe, event keyword
- Event patterns, pitfalls (memory leaks)