Let's say I have the following signature:
static extern void External(int foo, IntPtr bar);
I want to make it use defaults:
static extern void External(int foo = 10, IntPtr bar = default(IntPtr));
Is this valid? In C++, I开发者_Python百科 would use the pointer to be 0 or null. In C#, it's not even clear if IntPtr is value or reference.
If I called my function manually, I would use External(10, IntPtr.Zero);
. I guess my question is: Will default(IntPtr)
have the same behavior as IntPtr.Zero
?
IntPtr
is a value type, and its default is indeed IntPtr.Zero
. So this will work as you expect.
This MSDN page contains the following quote:
For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.
Since IntPtr
is a struct, its members will be initialized to 0.
精彩评论