Can I define 2 delegates with the same name but different parameters?

后端 未结 4 699
星月不相逢
星月不相逢 2020-12-06 14:08

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

public delegate int EnumWi         


        
4条回答
  •  忘掉有多难
    2020-12-06 14:14

    No, you cannot overload a delegate. Overloads are selected when there is type information available to the compiler to pick one... but with a delegate, you are supplying the type information and the compiler would have no way to select from overloads.

    If you want a family of similar delegate types, you can use generics.

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

    Now you can define overloaded p/invoke signatures which accept different delegate types EnumWindowsCallback, EnumWindowsCallback, etc.

提交回复
热议问题