我可以定义2名代表具有相同名称但不同的参数?定义、名称、不同、参数

2023-09-03 06:09:59 作者:还是一人 ╮

我试图定义的Int32 的IntPtr 之间的委托覆盖。为什么以下重载违法?

 公共委托INT EnumWindowsCallback(System.IntPtr的hWnd,INT lParam的);

市民代表INT EnumWindowsCallback(System.IntPtr的hWnd,System.IntPtr lParam的);
 

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

试想想起来了,我从来没有尝试过超载委托。它甚至合法的,如果是这样,为什么?

更新:在经历了答案,有的更是这样的帖子,我百思不得其解的代表不能即使有不同数量的参数声明。我仍然不知道为什么不能在运行时得到解决。

解决方案   

试想想起来了,我从来没有尝试过超载委托。它甚至合法的,如果是这样,为什么?

没有,这是不合法的。您目前正在申报两个类型的具有相同的完全限定名称。

excel2010如何使用定义名称

这的的唯一的事情看起来的有点像超载,当涉及到的类型是,如果你声明两种类型的泛型类型参数的数量而不同。例如,动作< T> 动作< T1,T2> 等为代表的规则比没有什么不同规则的其它类型这里。

因此​​,无论你需要声明一个泛型委托(并使用不同的类型参数),或使用两种不同的类型名称。

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?

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.

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.