none
Easy Question for experienced programmer! RRS feed

  • Question

  • I'm a newly student of C# and could someone please explain how to think on this question. Thanks!

    The following program has 3 overloaded methods. Which of the following gives the result 400?

    a) double fee = CalculateFee(200, 2);

    b) double fee = CalculateFee();

    c) double fee = CalculateFee(400);

    d) double fee = CalculateFee(400, 1);

    private static double CalculateFee(double dailyRate = 500.0, int numberOfDays = 1)

    {

    return dailyRate*numberOfDays;

    }

    private static double CalculateFee(double dailyRate = 500.0)

    {

    int defaultNoOfDays = 1;

    return dailyRate*defaultNoOfDays;

    }

    private static double CalculateFee()

    {

    double dailyRate = 400.0;

    int defaultNoOfDays = 1;

    return dailyRate*defaultNoOfDays;

    }

    Monday, April 8, 2019 4:39 PM

Answers

  • We don't normally provide answers to homework/quiz like questions so I'll just provide some guidance.

    The question really boils down to which overload each answer will return.

    a) Has two integral arguments so which overload matches that one?

    b) Has no arguments so which overload matches that one?

    c) Has one integral argument so which overload matches that one?

    d) Same as A

    What makes this question a little harder is that the overloads have default arguments. It is important to understand what overload resolution rules the compiler will follow. Ultimately the best thing you can do is put this code into the editor and run it so you see the results. The general rule of thumb for overload resolution:

    1) Prefer any overload that exactly matches the # and type of the parameters

    2) Prefer any overload that exactly matches the # of parameters and can coerce the arguments to the appropriate type.

    3) Prefer any overload that has default arguments that can fill in the missing arguments.

    Finally note that, in this particular case, the default arguments are not actually doing anything. Because there are overloads for 0, 1 or 2 arguments the default arguments on the methods would never be used. You could remove the default argument value (making the parameter required) and the behavior would be identical.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 8, 2019 5:28 PM
    Moderator

All replies

  • We don't normally provide answers to homework/quiz like questions so I'll just provide some guidance.

    The question really boils down to which overload each answer will return.

    a) Has two integral arguments so which overload matches that one?

    b) Has no arguments so which overload matches that one?

    c) Has one integral argument so which overload matches that one?

    d) Same as A

    What makes this question a little harder is that the overloads have default arguments. It is important to understand what overload resolution rules the compiler will follow. Ultimately the best thing you can do is put this code into the editor and run it so you see the results. The general rule of thumb for overload resolution:

    1) Prefer any overload that exactly matches the # and type of the parameters

    2) Prefer any overload that exactly matches the # of parameters and can coerce the arguments to the appropriate type.

    3) Prefer any overload that has default arguments that can fill in the missing arguments.

    Finally note that, in this particular case, the default arguments are not actually doing anything. Because there are overloads for 0, 1 or 2 arguments the default arguments on the methods would never be used. You could remove the default argument value (making the parameter required) and the behavior would be identical.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 8, 2019 5:28 PM
    Moderator
  • I'm curious to know, did they give you options for "all of the above" and "none of the above"?

    It's interesting that this same example would not have compiled in C++, because the the calls would be ambiguous.  Option (b) above, for example, could match ANY of the three functions.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, April 8, 2019 6:41 PM
  •  Cheers!
    Wednesday, April 10, 2019 2:42 PM
  • Which one/which(plural) answer gives you the result 400 i guess is the translation to English!
    Wednesday, April 10, 2019 2:46 PM
  • So it's either one correct or multiply!

    Wednesday, April 10, 2019 2:48 PM
  • Hi

    It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 6:40 AM
    Moderator