Given the following code, describe a situation that would make the code ambiguous to the C# compiler:using static System.Console;public class AmbiguousMethods{static void Main(){int iNum = 20;double dNum = 4.5;SimpleMethod(iNum, dNum); // calls first versionSimpleMethod(dNum, iNum); // calls second versionSimpleMethod(iNum, iNum); // error! Call is ambiguous.}private static void SimpleMethod(int i, double d){WriteLine("Method receives int and double");}private static void SimpleMethod(double d, int i){WriteLine("Method receives double and int");}}
What will be an ideal response?
In the Main() method above, a call to SimpleMethod() with an integer argument first and a double argument second executes the first version of the method, and a call to SimpleMethod() with a double argument first and an integer argument second executes the second version of the method. With each of these calls, the compiler can find an exact match for the arguments you send. However, if you call SimpleMethod() using two integer arguments, an ambiguous situation arises because there is no exact match for the method call. Because the first integer could be promoted to a double (matching the second version of the overloaded method), or the second integer could be promoted to a double (matching the first version), the compiler does not know which version of SimpleMethod() to use, and the program will not compile or execute.
You might also like to view...
A named range can begin with the / symbol
Indicate whether the statement is true or false
Clicking the New Slide Arrow enables you to change customized slide layouts that you created using the slide master
Indicate whether the statement is true or false
Which operator is used to determine if an object is of a particular class type?
A. The operator new B. The dot (.) operator C. The instanceof operator D. The + operator
The ________ feature automatically resizes windows when you drag them to the edge of the screen
Fill in the blank(s) with correct word