Methods group reusable logic. C# supports overloading (same name, different parameters), default arguments, ref/out parameters, and expression-bodied members—features beyond plain functions in C.
Overloading and defaults
int Add(int a, int b) => a + b;
double Add(double a, double b) => a + b;
void Greet(string name = "world") {
Console.WriteLine($"Hello, {name}");
}
static methods
static methods belong to the type, not an instance—Math.Max is a familiar example. Instance methods receive a hidden this reference like Java.
Important interview questions and answers
- Q: ref vs out?
A:refpasses an initialized variable by reference;outmust be assigned inside the method before return—common for TryParse patterns. - Q: Expression-bodied methods?
A:=> exprsyntax for single-expression members—still compile to normal methods.
Self-check
- Can two methods share a name if parameters differ?
- When would you mark a helper method static?
Pitfall: out parameters must be assigned before the method returns—compiler enforces this unlike optional Java-style patterns.
Interview prep
- ref vs out?
refpasses an initialized variable by reference;outmust be assigned inside the method before return—common for TryParse patterns.- Method overloading?
Same method name with different parameter lists—resolved at compile time by signature.