C#:将 null 传递给重载方法 - 调用哪个方法?

C#: Passing null to overloaded method - which method is called?(C#:将 null 传递给重载方法 - 调用哪个方法?)
本文介绍了C#:将 null 传递给重载方法 - 调用哪个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我有两个重载版本的 C# 方法:

Say I have two overloaded versions of a C# method:

void Method( TypeA a ) { }
void Method( TypeB b ) { }

我调用该方法:

Method( null );

调用了哪个方法的重载?我该怎么做才能确保调用特定的重载?

Which overload of the method is called? What can I do to ensure that a particular overload is called?

推荐答案

取决于TypeATypeB.

  • 如果其中一个适用(例如,没有从 nullTypeB 的转换,因为它是一个值类型,但 TypeA 是一个引用类型),然后将调用适用的类型.
  • 否则取决于TypeATypeB之间的关系.
    • 如果存在从 TypeATypeB 的隐式转换,但没有从 TypeBTypeA 的隐式转换,则将使用使用 TypeA 的重载.
    • 如果存在从 TypeBTypeA 的隐式转换,但没有从 TypeATypeB 的隐式转换,则将使用使用 TypeB 的重载.
    • 否则,调用是不明确的,将无法编译.
    • If exactly one of them is applicable (e.g. 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 applicable one.
    • Otherwise it depends on the relationship between TypeA and TypeB.
      • 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 used.
      • 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 used.
      • Otherwise, the call is ambiguous and will fail to compile.

      有关详细规则,请参阅 C# 3.0 规范的第 7.4.3.4 节.

      See section 7.4.3.4 of the C# 3.0 spec for the detailed rules.

      这是一个没有歧义的例子.这里TypeB 派生自TypeA,这意味着从TypeBTypeA 的隐式转换,但反之则不然.因此使用了使用 TypeB 的重载:

      Here's an example of it not being ambiguous. Here TypeB derives from TypeA, which means there's an implicit conversion from TypeB to TypeA, but not vice versa. Thus the overload using TypeB is used:

      using System;
      
      class TypeA {}
      class TypeB : TypeA {}
      
      class Program
      {
          static void Foo(TypeA x)
          {
              Console.WriteLine("Foo(TypeA)");
          }
      
          static void Foo(TypeB x)
          {
              Console.WriteLine("Foo(TypeB)");
          }
      
          static void Main()
          {
              Foo(null); // Prints Foo(TypeB)
          }
      }
      

      一般来说,即使面对其他不明确的调用,为了确保使用特定的重载,只需强制转换:

      In general, even in the face of an otherwise-ambiguous call, to ensure that a particular overload is used, just cast:

      Foo((TypeA) null);
      

      Foo((TypeB) null);
      

      请注意,如果这涉及声明类中的继承(即一个类正在重载由其基类声明的方法),您将陷入另一个问题,您需要转换方法的目标而不是参数.

      Note that if this involves inheritance in the declaring classes (i.e. one class is overloading a method declared by its base class) you're into a whole other problem, and you need to cast the target of the method rather than the argument.

      这篇关于C#:将 null 传递给重载方法 - 调用哪个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

      本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)