Optional Parameters in C#.NET

And now, there’s an interesting lesson called OPTIONAL PARAMETERS that we all need to learn in C#.NET.

Normally, we write our Actual methods which some thing looks like:
                        
Public void someMethod(string userName, string password){ }

And we call/pass the arguments as:
                        someMethod(“user1”,”password@user1”);

i.e., we pass the arguments from the calling method in the same way as of we’ve declared. But what if we have an option to pass the variables(irrespective of orders)?? And this flexible option is given to us by Optional Parameters. So now I can pass the variables in some way like:

                        someMethod(“password@user1”,”user1”);

But to let the compiler know which argument you are passing, we need to prefix the argument with the variable name(which is declared in the Actual Method/Called Method). So now I can write as:

                        someMethod(userName: “user1”, password: “password@user1”);
                                                            or
                        someMethod(password: “password@user1”, userName: “user1”);

So, finally we got to know that how flexible the optional parameters are and that’s even pretty cool… 

And another interesting part of optional parameters is:
            We can initialize the values of the variables in the Actual Method itself, which would something look like:
            Public void someMethod(int x, int y=10, int z=20){ }

So, the magic here is, its not mandatory that we need to pass all the three arguments(except for the non-optional parameters) . So, now I can make a call the above method like:

                                    1:          someMethod(x: 5);
                                                            or
                                    2:          someMethod(x:5, y:20);
                                                            or
                                    3:          someMethod(x:5, y:20, z:30);

All the above mentioned ways are possible while invoking the function.
So, in the case 1: the values would be:  x=5, y=10, z=20 (at the called method). This is because y and z are optional parameters and its not mandatory to pass the values to them...

And in the case 2: the values at the called method would be: x=5, y=20, z=20. This is because as y is an optional parameter its up to us whether to pass the values or not. If we pass a value from the calling method, then that would override the default value( y=20 in this case) and if it is not passed, the default value will be present.
And in the last case(3): The values at the called method would be: x=5, y=20, z=30. The reason is same as of we discussed with case 2.

And this is all pretty about Optional Parameters...J

Note: The convention here to be followed is that we need to  declare the optional parameters after all the required parameters i.e., we can have something like:

                                    Public void someMethod(int x, int y=10){ }

but we cannot have something like:
                                    public void someMethod(int x=10,y) { }

So if you try to declare a method which is similar to the last one, the compiler would throw us an error stating: “Optional parameters must appear after all required parameters”.


So, the usage of this optional parameters would serve us better at times… J

No comments:

Post a Comment