Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If i am using function overloading here why it's calling function which has string as input parameter.
Although object can also accepts null.

What I have tried:

public void show(object a)
        {
			console.writeline("print object");
        }
        
		
		
		static void Main(string[] args)
        {
			show();
		}
		
		Output //print object
		
		public void show(string a)
        {
			console.writeline("print string");
        }
		
		 public void show(object a)
        {
			console.writeline("print object");
        }
        
		
		
		static void Main(string[] args)
        {
			show();
		}
		Output //print string
Posted
Updated 16-Feb-17 22:12pm

 
Share this answer
 
Because you are always calling the same version - the version that doesn't take any parameters.
Try this:
public void show()
    {
    Console.WriteLine("print no parameter");
    }
public void show(object a)
    {
    Console.WriteLine("print object");
    }
public void show(string a)
    {
    Console.WriteLine("print string");
    }
		
static void Main(string[] args)
    {
    show();
    show(123);
    show("123");
    }
And it should work as you expect.


[edit]
If Karthik Bangalore is right, and you want to know why a null parameter causes the string version to be called, then the link he gives doesn't tell the whole story; it's a little more complicated than that!

If you have two overloads:
C#
public void show(TypeA a){}
public void show(TypeB b){}

And you call it with a null value, then the .NET runtime has to decide which is most appropriate, and it works like this:
If there is no conversion from null to TypeB because it's a value type but TypeA is a reference type then the call will be made to the TypeA method.
Otherwise, if there is an implicit conversion from TypeA to TypeB but no implicit conversion from TypeB to TypeA then the overload using TypeA will be called.
If there is an implicit conversion from TypeB to TypeA but no implicit conversion from TypeA to TypeB then the overload using TypeB will be called.
Otherwise, the call is ambiguous and will fail.

When you compare string and object, there is an implicit conversion from string (because all strings are also objects) but not the other way (because not all objects are strings) so the runtime calls the string version.
[/edit]
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 17-Feb-17 4:33am    
Hi OriginalGriff
if my understanding is rite, the OP is trying to ask
if we call show(null); why does it print "string" instead of "object" since string and object's default value is null
OriginalGriff 17-Feb-17 4:58am    
Ah! Could be: answer updated.
Karthik_Mahalingam 17-Feb-17 5:01am    
:)
hashir khan 2022 23-Aug-22 3:58am    
Write a program of function overloading that has a function Convert() that accepts a value in parameter and convert them into string and print on screen.
OriginalGriff 23-Aug-22 4:11am    
OK ... done.

What is your next order, oh master?

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