C# and .NET • Modern C# baseline
Methods, Parameters, and Return Values
Design methods with clear input, output, and mutation behavior.
A method signature is a contract between a caller and an implementation. Use value parameters for small values and immutable data, and avoid ref or out unless the interface genuinely needs them. Prefer returning a result object or tuple when a method needs to produce several related values.
Key points
- Use descriptive names for parameters and return values.
- Keep methods focused enough that callers can test their behavior independently.
Example
static decimal CalculateTotal(int quantity, decimal unitPrice)
{
if (quantity < 0 || unitPrice < 0)
{
throw new ArgumentOutOfRangeException(
quantity < 0 ? nameof(quantity) : nameof(unitPrice));
}
return quantity * unitPrice;
}
What to expect
CalculateTotal(3, 2.50m) returns 7.50.
Practice exercise
Refine validation so the exception identifies whichever argument was invalid.
Key idea: Make invalid calls fail clearly and keep method effects predictable.