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

Leave a comment

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