C#中函数重载的问题 [英] Problems with function overloading in C#

查看:53
本文介绍了C#中函数重载的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在这里使用函数重载,为什么它调用函数将字符串作为输入参数。

虽然对象也可以接受null。



我尝试过:



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

推荐答案

检查这个类似的问题&回答 .net - 将空值传递给重载方法,其中Object和String作为C#中的参数 - Stack Overflow [ ^ ]
check this similar question & answered .net - Passing null value to overloading method where Object and String as param in C# - Stack Overflow[^]


因为你总是调用相同的版本 - 不带任何参数的版本。

试试这个:

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");
    }

它应该按预期工作。





[edit]

如果Karthik Bangalore是对的,并且你想知道为什么null参数会导致调用字符串版本,那么他给出的链接并不能说明整个故事;它有点复杂!



如果你有两个重载:

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:

public void show(TypeA a){}
public void show(TypeB b){}



你用空值调用它,然后是.NET运行时必须决定哪个最合适,它的工作方式如下:

如果没有从null转换为TypeB,因为它是一个值类型但TypeA是引用类型,那么调用将是使用TypeA方法。

否则,如果存在从TypeA到TypeB的隐式转换,但没有从TypeB到TypeA的隐式转换,则将调用使用TypeA的重载。

如果存在从TypeB到TypeA的隐式转换但没有从TypeA到TypeB的隐式转换,那么将调用使用TypeB的重载。

否则,调用是不明确的并且将会失败。



比较字符串和对象时,会有一个字符串的隐式转换(因为所有字符串也是对象),但不是另一种方式(因为不是所有对象)是字符串)所以运行时调用字符串版本。

[/ edit]


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]


这篇关于C#中函数重载的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆