Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Overloading Versus Optional Parameters

I seem to recall reading there was an important difference between method overloading (and constructor chaining) and optional parameters in C# 4.0, but I haven't been able to locate anything acknowledging any difference.

Are there any important differences between the following two implementations?

First

public void Foo()
{
   Foo(String.Empty);
}

public void Foo(string message)
{
   Console.WriteLine(message);
}

Second

public void Foo(string message = "")
{
   Console.WriteLine(message);
}
like image 515
senfo Avatar asked Mar 02 '26 03:03

senfo


1 Answers

I would favour method overloading. There are known versioning issues with optional parameters.

There is a very good article by Jon Skeet here.

Motivation for adding this was making it easier to talk to COM where methods can have many many parameters and less fora new design practice for C# classes

like image 61
Aliostad Avatar answered Mar 04 '26 17:03

Aliostad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!