public int foo(int x)
    int defaultValue = 0;
    return foo(x, defaultValue);

public int foo(int x, int y)
    return x + y;
  • 2
    If this is a pattern then you could as well call "i++" the "index incrementation pattern". – Kim Feb 10 '11 at 21:21
  • 1
    @Kim touche.... – Steve Feb 10 '11 at 21:42

It's called emulating default argument through method overloading.

And where does it use polymorphism?

  • @delnan overloading = polymorphism – Steve Feb 10 '11 at 20:52
  • @Steve: No. Unless you mean "overloading on this", which would be a strange definition, would only work if overloads weren't decided statically but dynamically and doesn't apply here anyway. – user7043 Feb 10 '11 at 20:59
  • 1
    Specifically method overloading - having multiple methods with the same name but different signatures - is (sometimes) referred to as ad hoc polymorphism. – Frank Shearar Feb 10 '11 at 21:00
  • 3
    @Frank: Ah, yes. But that's not what polymorphism usually describes in OOP context. – user7043 Feb 10 '11 at 21:01
  • @Frank: "ad-hoc polymorphism" describes overriding, not overloading. "ad-hoc" is used to disambiguate between type polymorphism (what people call "generics" in OOP languages) and overriding, not between overriding and overloading. Static overloading just isn't polymorphism at all. – munificent Feb 10 '11 at 23:50

Overloading is not polymorphism.

I'm not really sure of a pattern, except that it's a way to create overloaded constructors with default parameters.

  • I was under the assumption that static polymorphism is method overloading. Anyway we digress. – Steve Feb 10 '11 at 21:11

In .NET 4 this type of coding is not necessary. Instead, use optional parameters with default values. More info.

  • 3
    A bite of missing-the-question anyone? ;) – user7043 Feb 10 '11 at 21:32
  • I wouldn't call the code in the OP a design pattern (not to be confused with a design principle). Patterns are proven techniques that can be applied in a set of scenerios. The code is recognizeable as an obsolete technique for new development, if anything that makes it an anti-pattern. Thus, the answer. – P.Brian.Mackey Feb 10 '11 at 22:17
  • It's hardly obsolete, and optional parameters are a tradeoff, not a clear win. But the word you're probably looking for is "idiom". – Frank Shearar Feb 11 '11 at 8:49
  • @Frank shearar - point taken. Good info on this subject stackoverflow.com/questions/3316402/… – P.Brian.Mackey Feb 12 '11 at 0:29
  • @P.Brian.Mackey: Even in .NET 4, that style of overloading is still necessary in cases where the default value for a parameter cannot be expressed as a compile-time constant. – supercat May 8 '14 at 17:12

"DRY Overloading" is a better description; polymorphism refers to inheritance hierarchies. I don't think you could call it a design or architecture pattern, either, since details at this level are details of implementation, not design.


The code you show is an example of how to implement default parameter values using a language that does not permit default parameter values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.