开发者

Delegate with ref parameter

开发者 https://www.devze.com 2022-12-13 15:00 出处:网络
Is there any way to maintain the same functionality in the code below, but without having to create the delegate? I\'m interfacing with a 3rd-party API that contains a number of various DeleteSomethin

Is there any way to maintain the same functionality in the code below, but without having to create the delegate? I'm interfacing with a 3rd-party API that contains a number of various DeleteSomethingX(ref IntPtr ptr) methods and I'm trying to centralize the code for the IntPtr.Zero check.

private void delegate CleanupDelegate(ref IntPtr ptr);

...

private void Cleanup(ref IntPtr ptr, CleanupD开发者_如何学Celegate cleanup)
{
    if (ptr != IntPtr.Zero)
    {
        cleanup(ref ptr);
    }
}


If you mean without declaring the delegate type, then probably not; very few (if any) inbuilt delegates use ref; but you could make it generic:

delegate void ActionRef<T>(ref T value);

I'm not sure this saves much though. There may also be some tricks here with extension methods, but it is hard to tell without more detail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号