wasAsked.

Explain method overloading.

PwC · Role not specified · technical

Published

Asked in 2026 · C#

Community answers

Posted by wasAsked editorial collection

Method overloading defines methods with the same name but different parameter lists. The compiler selects the best applicable overload using compile-time types and conversion rules. Return type alone cannot distinguish overloads. Example: public string Format(decimal value) => value.ToString("0.00"); public string Format(decimal value, string currency) => string.Concat(currency, " ", value.ToString("0.00")); public string Format(decimal value, CultureInfo culture) => value.ToString("C", culture); Avoid overloads where null, optional parameters, params arrays, or similar numeric conversions make calls ambiguous. Overloading is compile-time polymorphism; overriding is runtime dispatch through a virtual or abstract contract.

Backbencher Developer · · 0 likes

Method overloading means defining several methods with the same name in the same class (or across a class and its base), but with different parameter lists. The compiler chooses which one to call based on the arguments — this is compile-time (static) polymorphism. Valid ways to overload - Different number of parameters. - Different parameter types. - Different order of parameter types. - Different ref, out or in modifiers (but not differing only between ref and out). Not valid - Differing only by return type. - Differing only by parameter names. - Differing only by params versus a plain array. Example: public class ReportService { public Report Generate(int year) => Generate(year, 1, 12); public Report Generate(int year, int month) => Generate(year, month, month); public Report Generate(int year, int fromMonth, int toMonth) { /* main implementation */ return new Report(); } public Report Generate(DateOnly from, DateOnly to) { /* date-range version */ return new Report(); } } var r1 = service.Generate(2026); var r2 = service.Generate(2026, 11); var r3 = service.Generate(new DateOnly(2026, 1, 1), new DateOnly(2026, 3, 31)); Real framework examples: Console.WriteLine has many overloads (string, int, object, format strings), and string.Split has overloads for char, string and options. Overload resolution details - The compiler picks the "best" match; exact matches beat implicit conversions (int to long), which beat boxing. - Ambiguity causes a compile error, for example when null could match two reference-type overloads. - Optional parameters and params arrays also take part, and can make overloads ambiguous or surprising — use them carefully. Overloading vs overriding - Overloading: same name, different parameters, same or related class, resolved at compile time. - Overriding: same signature, derived class replaces a virtual base method, resolved at runtime. Best practices - Overloads should do the same conceptual thing; delegate to one main implementation to avoid duplicated logic. - Prefer optional parameters or an options object when the list of overloads grows large. - Constructors can be overloaded too, often chained with this(...).

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics