Signatures, returns; expr-bodied; optional/named; ref/out; overloading
Define method signatures and return values, use expression-bodied methods, optional/named parameters, ref/out, and understand overloading. Note: no local functions in C# 6.
Signatures, returns; expr-bodied; optional/named; ref/out; overloading 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.
Method basics
Goal: Define clean methods: signature (name, parameters, return type), concise bodies, and predictable behavior.
- Return values
- Expression-bodied methods
- Optional/named params
Signatures & returns
A method has a name, parameters, and a return type. Keep names clear and focused.
using System;
public class MathUtil
{
// Signature: name Add, parameters (int a, int b), return int
public static int Add(int a, int b)
{
return a + b;
}
}
public class Program
{
public static void Main(string[] args)
{
int s = MathUtil.Add(2, 3);
Console.WriteLine("Sum = " + s);
}
}
Expr-bodied methods
Expression-bodied methods use => for tiny, clear helpers.
using System;
public static class FormatUtil
{
// Expression-bodied method (C# 6): concise one-liner
public static string Greet(string name) => "Hello, " + name + "!";
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(FormatUtil.Greet("Ada"));
}
}
Optional & named
Use optional params for sensible defaults and named args for clarity at the call site.
using System;
public static class Logger
{
// Optional parameter: times has a default
public static void Log(string message, int times = 1)
{
for (int i = 0; i < times; i = i + 1)
{
Console.WriteLine(message);
}
}
}
public class Program
{
public static void Main(string[] args)
{
Logger.Log("One time"); // uses default
Logger.Log(message: "Twice", times: 2); // named arguments
}
}
ref & out basics
ref: variable must be assigned before call. out: method must assign before returning.
using System;
public static class RefOutDemo
{
public static void Increment(ref int x)
{
x = x + 1; // modifies caller's variable
}
public static bool TryDivide(int a, int b, out int result)
{
if (b == 0)
{
result = 0; // out must be assigned before return
return false;
}
result = a / b;
return true;
}
}
public class Program
{
public static void Main(string[] args)
{
int n = 10;
RefOutDemo.Increment(ref n);
Console.WriteLine("n = " + n); // 11
int r;
bool ok = RefOutDemo.TryDivide(9, 0, out r);
Console.WriteLine("ok=" + ok + " r=" + r);
}
}
Overloading & note
Overloading reuses a name for related operations. Note: local functions appear in later C# versions; in C# 6 use small private helpers.
using System;
public static class Calc
{
// Overloads: same name, different parameter lists
public static int Area(int side) { return side * side; }
public static int Area(int width, int height) { return width * height; }
// C# 6 has no local functions; use private helpers instead.
private static int DoubleIt(int x) { return x + x; }
public static int TwiceArea(int side)
{
int a = Area(side);
return DoubleIt(a);
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Area square 3 = " + Calc.Area(3));
Console.WriteLine("Area rect 3x4 = " + Calc.Area(3, 4));
Console.WriteLine("Twice area of 3 = " + Calc.TwiceArea(3));
}
}
ref vs out rule
Recap
Recap: Design clear signatures, return values, and tiny expression-bodied helpers; use optional/named params for readability; know ref vs out; overload carefully. Local functions come in later C#—use private helpers in C# 6.
Frequently asked questions
Is the “Signatures, returns; expr-bodied; optional/named; ref/out; overloading” lesson free?
Yes — the full text of “Signatures, returns; expr-bodied; optional/named; ref/out; 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 “Signatures, returns; expr-bodied; optional/named; ref/out; overloading”?
Define method signatures and return values, use expression-bodied methods, optional/named parameters, ref/out, and understand overloading. Note: no local functions in C# 6. 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 “Signatures, returns; expr-bodied; optional/named; ref/out; 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