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.
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);
}
}
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