Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if i have two method
C#
void method(string str="")
{
//logic
}

void method()
{
//logic
}

and i am calling it

void main()
{
method()
}

which one is called?

What I have tried:

interview question that is asked it gives any error runtime or compile time?
Posted
Updated 19-Oct-16 4:13am
v2
Comments
Bernhard Hiller 19-Oct-16 3:38am    
Why don't you just open up VS and paste that code there?
johannesnestler 19-Oct-16 8:25am    
interesting question, but why ask it here? - what you do during programming work if you don't know: Try it! or read MSDN: https://msdn.microsoft.com/en-us/library/dd264739.aspx (have a look at 'overload resolution' at the bottom of the page)

method' is ambiguous '
Candidates are:
void method()
void method(std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>)
'
 
Share this answer
 
Comments
#realJSOP 19-Oct-16 10:18am    
This is a C# question, not a C++ question. Your answer is irrelevant.
yes, i have try out it and i got the answer. it is simple overloading mechanism and c# complier call parameter less mathode first. it will not give any error. work fine.
thanks
 
Share this answer
 
So does anyone know a way to force the use of the method with the default parameter?

I thought of a solution with a special delegate for this case - But this doesn't work like I first thought: look how this behaves...


C#
class Program
 {
     delegate void methodDelegate(string str = "ABC");

     static void Main()
     {
         method();
         methodDelegate methodWithOptionalParameter = method;
         methodWithOptionalParameter.Invoke();

         Console.ReadKey();
     }

     static void method(string str = "XYZ")
     {
         Console.WriteLine("method(string str = \"{0}\")", str);

     }

     static void method()
     {
         Console.WriteLine("method()");
     }

 }


(hint: it uses the default value from the delegate Definition)
 
Share this answer
 
v3
If you're interviewing for a programming job, and it never occurred to you to try it yourself and see, do you honestly think you've selected an appropriate career path?
 
Share this answer
 
Create a console application and run this code to see which method is being called.

C#
using System;

class Program
{

    static void Main()
    {
        method();
        Console.ReadLine();
    }

    static void method(string str = "")
    {
        Console.WriteLine("method(string str = \"\")");

    }

    static void method()
    {
        Console.WriteLine("method()");
    }
}
 
Share this answer
 
Comments
CPallini 19-Oct-16 6:34am    
5.Experimental evidence :-)
Karthik_Mahalingam 19-Oct-16 12:15pm    
Thank you CPallini :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900