Is the default value (IntPtr) legal in the extern function?

Say I have the following signature:

static extern void External(int foo, IntPtr bar);

I want to use it by default:

static extern void External(int foo = 10, IntPtr bar = default(IntPtr));

It's really? In C ++, I would use a pointer equal to 0 or null. In C #, it is not even clear if IntPtr is a value or reference.

If I called my function manually, I would use External(10, IntPtr.Zero);. I guess my question is: will it default(IntPtr)have the same behavior as IntPtr.Zero?

+3
source share
1 answer

IntPtris the type of value, and by default it is valid IntPtr.Zero. This way, it will work as you expect.

This MSDN page contains the following quote:

, , , .

IntPtr , 0.

+9

Source: https://habr.com/ru/post/1785319/


All Articles