When invoking a function, can you not pass in values as mentioned in the method signature and pass in only the values you need ???
Sorry !!! you cannot do that. But what if you need to do so ??? Optional Parameters is the answer. Optional Parameters allows you to define a method and provide default values for parameters in the parameter list.
Note : All the declared optional parameters should be at the end of the parameter list. In other words it should be declared after the normal parameters.
// Method declaration
int OptionalTest (int a, int b, int c = 10)
{
...
}
// Method invocation
int _val = OptionalTest(10, 5); // ' c ' would be 10
int _val = OptionalTest(10, 5, 25); // ' c ' would be 25
Knew about the default parameter values. But didn't know, that they're optional also !!
ReplyDeleteCheers (Y)