C# 方法解析,long vs int [英] C# Method Resolution, long vs int

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

问题描述

class foo{public void bar(int i) { ... };public void bar(long i) { ... };}foo.bar(10);

我希望这段代码会给我一些错误,或者至少是一个警告,但事实并非如此......

调用什么版本的 bar(),为什么?

解决方案

bar 的 int 版本被调用,因为 10 是一个 int 字面量,编译器会寻找最匹配的方法输入变量.要调用长版本,您需要指定一个长文本,如下所示:foo.bar(10L);

这是 Eric Lippert 发表的关于方法重载的更复杂版本的帖子.我会尝试解释它,但他做得更好,我永远可以:http://blogs.msdn.com/b/ericlippert/archive/2006/04/05/odious-ambiguous-overloads-part-one.aspx

来自 C# 4.0 规范:

<块引用>

方法重载允许多个同一个类中的方法相同的名称,只要它们具有唯一性签名.当编译一个调用重载方法,编译器使用重载决议确定具体方法调用.重载解析找到一种最匹配的方法参数或如果没有则报告错误可以找到单个最佳匹配.这以下示例显示重载决议生效.评论为Main 方法中的每次调用显示哪个方法实际上是调用.

 类测试 {静态无效 F() {Console.WriteLine("F()");}静态无效 F(对象 x){Console.WriteLine("F(object)");}静态无效 F(int x) {Console.WriteLine("F(int)");}静态无效 F(双 x){Console.WriteLine("F(double)");}静态空隙 F T (T x) {Console.WriteLine("F(T)");}静态无效 F(双 x,双 y){Console.WriteLine("F(double,double)");}静态无效主(){F();//调用 F()F(1);//调用 F(int)F(1.0);//调用 F(double)F("abc");//调用 F(object)F((双)1);//调用 F(double)F((对象)1);//调用 F(object)F int (1);//调用 F<T>(T)F(1, 1);//调用 F(double, double)}}

<块引用>

如示例所示,一个特定的方法总是可以通过将参数显式转换为确切的参数类型和/或显式提供类型参数.

class foo
{
  public void bar(int i) { ... };
  public void bar(long i) { ... };
}


foo.bar(10);

I would expect this code to give me some error, or at least an warning, but not so...

What version of bar() is called, and why?

解决方案

The int version of bar is being called, because 10 is an int literal and the compiler will look for the method which closest matches the input variable(s). To call the long version, you'll need to specify a long literal like so: foo.bar(10L);

Here is a post by Eric Lippert on much more complicated versions of method overloading. I'd try and explain it, but he does a much better job and I ever could: http://blogs.msdn.com/b/ericlippert/archive/2006/04/05/odious-ambiguous-overloads-part-one.aspx

from the C# 4.0 Specification:

Method overloading permits multiple methods in the same class to have the same name as long as they have unique signatures. When compiling an invocation of an overloaded method, the compiler uses overload resolution to determine the specific method to invoke. Overload resolution finds the one method that best matches the arguments or reports an error if no single best match can be found. The following example shows overload resolution in effect. The comment for each invocation in the Main method shows which method is actually invoked.

 class Test {   
      static void F() {
        Console.WriteLine("F()");   
      }     
      static void F(object x) {
        Console.WriteLine("F(object)");     
      }
      static void F(int x) {
        Console.WriteLine("F(int)");    
      }
      static void F(double x) {
        Console.WriteLine("F(double)");     
      }
      static void F<T>(T x) {
        Console.WriteLine("F<T>(T)");   
      }
      static void F(double x, double y) {
        Console.WriteLine("F(double,double)");  
      }     

      static void Main() {
        F();                // Invokes F()
        F(1);           // Invokes F(int)
        F(1.0);         // Invokes F(double)
        F("abc");       // Invokes F(object)
        F((double)1);       // Invokes F(double)
        F((object)1);       // Invokes F(object)
        F<int>(1);      // Invokes F<T>(T)
        F(1, 1);        // Invokes F(double, double)
      } 
}

As shown by the example, a particular method can always be selected by explicitly casting the arguments to the exact parameter types and/or explicitly supplying type arguments.

这篇关于C# 方法解析,long vs int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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