Methods are functions defined in a class. C# supports rich parameter features — optional/named arguments, ref/out/in modifiers, params arrays — and (multiple methods with the same name but different parameters).
public int Add(int a, int b) => a + b; // expression-bodied (concise)
public string Greet(string name, string greeting = "Hello") // default parameter
{
return $"{greeting}, {name}";
}
Greet("Ann"); // "Hello, Ann"
Greet("Ann", greeting: "Hi"); // named argument
Methods specify a return type, name, and parameters. Default parameters make arguments optional, and named arguments let you pass by name (clearer, can skip optionals).
// ref — pass by reference (method can modify the caller's variable)
void Increment(ref int x) { x++; }
int n = 5; Increment(ref n); // n is now 6
// out — for returning multiple values (must be assigned in the method)
bool TryParse(string s, out int result) { result = ...; return true; }
if (int.TryParse("42", out int value)) { } // the common TryX pattern
// in — pass by reference but READ-ONLY (performance for large structs)
void Process(in LargeStruct data) { }
out is widely used in the TryParse pattern (returning a success flag plus the result safely); ref allows modifying the caller's variable; in passes large structs efficiently without copying.
// params — variable number of arguments
int Sum(params int[] numbers) => numbers.Sum();
Sum(1, 2, 3, 4); // any number
// OVERLOADING — same name, different parameters
void Print(int x) { }
void Print(string s) { }
void Print(int x, int y) { }
Print(5); // calls the int version — resolved by argument types
Overloading lets multiple methods share a name, distinguished by parameter types/count — the compiler picks the matching one. params accepts a variable number of arguments.
Methods are the building blocks of behavior in C#, and understanding their rich parameter features is important for writing flexible, expressive code.
Knowing how to define methods (including concise expression-bodied syntax), use default and named parameters (for optional arguments and readable calls), and apply method overloading (same name, different parameters — for intuitive APIs that handle different argument types) is everyday knowledge.
The parameter modifiers are particularly important to understand: out is widely used in the ubiquitous TryParse pattern (int.TryParse(s, out var result) — safely returning a success flag and result without exceptions, a common, idiomatic C# pattern), ref enables modifying the caller's variable, and in passes large structs by reference for performance.
Understanding these — methods, the parameter features (optional/named/params), overloading for flexible APIs, and the ref/out/in modifiers (especially the out-based TryParse idiom) — is fundamental for both writing C# methods and using the many .NET APIs that rely on these features.
Since methods and their parameters are central to all C# code, and since features like overloading, optional parameters, and the TryX pattern appear constantly, mastering them is core, must-know knowledge for effective C# development.