我可以定义 2 个名称相同但参数不同的委托吗?

Can I define 2 delegates with the same name but different parameters?(我可以定义 2 个名称相同但参数不同的委托吗?)
本文介绍了我可以定义 2 个名称相同但参数不同的委托吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Int32IntPtr 之间定义一个委托覆盖.为什么以下重载是非法的?

I tried to define a delegate override between Int32 and IntPtr. Why are the following overloads illegal?

public delegate int EnumWindowsCallback (System.IntPtr hWnd, int lParam);

public delegate int EnumWindowsCallback (System.IntPtr hWnd, System.IntPtr lParam);

这看起来很奇怪.它们都是结构,但不同,并从不同的接口实现.

That looks pretty strange. They are both structs but are different and implement from different interfaces.

想一想,我以前从未尝试过重载委托.这是否合法,如果是,为什么?

Come to think of it, I have never tried to overload a delegate before. Is it even legal, and if so, why?

更新:在查看了答案和更多 SO 帖子后,我感到困惑的是,即使使用不同数量的参数也无法声明代表.我仍然想知道为什么不能在运行时解决这个问题.

UPDATE: After going through the answers and some more SO posts, I was baffled that delegates cannot be declared even with a varying number of parameters. I am still wondering why this cannot be resolved at runtime.

推荐答案

想一想,我以前从未尝试过重载委托.这是否合法,如果是,为什么?

Come to think of it, I have never tried to overload a delegate before. Is it even legal, and if so, why?

不,这是不合法的.您当前正在声明两个具有相同完全限定名称的类型.

No, it's not legal. You're currently declaring two types with the same fully-qualified name.

在类型方面看起来唯一有点像重载的是,如果您声明了两个泛型类型参数数量不同的类型.比如Action<T>Action<T1, T2>等.这里的delegate的规则和其他类型的规则没有区别.

The only thing that looks a bit like overloading when it comes to types is if you declare two types which differ in the number of generic type parameters. For example, Action<T>, Action<T1, T2> etc. The rules for delegates are no different than the rules for other types here.

因此,要么您需要声明一个泛型委托(并使用不同的类型参数),要么使用两个不同的类型名称.

So either you need to declare one generic delegate (and use different type arguments), or use two different type names.

这篇关于我可以定义 2 个名称相同但参数不同的委托吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)