方法重载和空值 [英] Method overloading and null value

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

问题描述

可能重复:结果
C#:传递null到重载的方法 - 这方法被称为

考虑以下2种方法:

void Method(object obj)  { Console.WriteLine("object"); }
void Method(int[] array) { Console.WriteLine("int[]"); }

当我尝试调用:

Method(null);



在Visual Studio 2008 SP1,我得到 INT []

这是为什么呢?

推荐答案

这是一个重载的产品。你的说法,,可以转换到两个对象 INT [] 。因此,编译器选取最具体的版本,因为 INT [] 对象

It is a product of overload resolution. Your argument, null, is convertible to both object and int[]. The compiler therefore picks the most specific version, because int[] is more specific than object.

这篇关于方法重载和空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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