C# – Creating Overloaded Methods and Optional Paramaters

By creating overloaded mehods, we are mentioning different METHOD SIGNATURE but same function. METHOD SIGNATURE… the number and type of input parameters can have different return types.

Optional parameters provide default values when you define the method. So,  if you don’t supply a value, the default will be used.

private void myMethod(string myRequiredParam, int myOptionalParam = 1, int myOptionalParam = 2);

can be called…..

myMethod("Hello Required Param");

or

myMethod("Hello Required Param", 100);

or

myMethod("Hello Required Param", 100, 500);

You can not skip an optional parameter

myMethod("Hello Required Param",  , 500);//ERROR
Advertisement

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: