We’ve learnt that using the named parameters we can make a call to any function and pass the arguments as we wished to; like:
If we want to call the above method, we can make a call to the function in any of the two ways as shown below:
That’s all fine but if we have overloaded method for the above method?; like:
This is all acceptable because C# allows us to define overloaded methods. But there raises an ambiguousity when we make call to the either of the methods using Named Parameters.
Let us consider a call to above overloaded methods as:
When we make a call this way, the compiler throws an exception stating: “The call is ambiguous between the following methods or properties”..!!! So, consider not to use Named Parameters when you are dealing with Overloaded Methods/Constructors as that would raise many issues..!!
Note: If we give the different names for the arguments in the overloaded methods, then its possible to use Named Parameters..
SomeMethod(int a, float b){ }
If we want to call the above method, we can make a call to the function in any of the two ways as shown below:
SomeMethod(a:2, b:2.1F);
Or
SomeMethod(b:2.1F, a:2);
Or
SomeMethod(b:2.1F, a:2);
That’s all fine but if we have overloaded method for the above method?; like:
SomeMethod(float b, int a){ }
This is all acceptable because C# allows us to define overloaded methods. But there raises an ambiguousity when we make call to the either of the methods using Named Parameters.
Let us consider a call to above overloaded methods as:
SomeMethod(b:2.1F, a:2);
When we make a call this way, the compiler throws an exception stating: “The call is ambiguous between the following methods or properties”..!!! So, consider not to use Named Parameters when you are dealing with Overloaded Methods/Constructors as that would raise many issues..!!
Note: If we give the different names for the arguments in the overloaded methods, then its possible to use Named Parameters..
No comments:
Post a Comment