Learning: creating overloads of function with params type parameter


This is about mistake which happened couple of days ago. 
We had a function having declaration like:
protected string function1(string parameter1, params string[] parameterArray)
{
//some code
}

Due to some requirement to overload this API  we added below function 

protected string function1(string parameter1, string parameter2, params string[] parameterArray)
{
//some code
}

Now if we compile these two functions the project will be compiled with no errors and also the calling function will be compiled successfully.
But for the cases where code was calling function1 using below syntax may give some undesired results.

function1(“some string“, “some string1“, “some string2“, “some string3“);

Reason:  Now the call is getting diverted to best available match which is our newly added API.

We should be extra carful about creating overloading funcitons specially when current function is having any of the params type parameter.
For above discussed case best way is to create a new API as chaning old API may end up in breaking some calling codes.

Advertisements

One thought on “Learning: creating overloads of function with params type parameter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s