I have a couple of methods imported from a native .dll, using the following syntax:
internal static class DllClass {
[DllImport("Example.dll", EntryPoint = "ExampleFunction")]
public static extern int ExampleFunction([Out] ExampleStruct param);
}
Now, because I specified param
as [Out]
, I would expect at least one of the following snippets to be valid:
ExampleStruct s;
DllCass.ExampleFunction(s);
ExampleStruct s;
DllCass.ExampleFunction([Out] s);
ExampleStruct s;
DllCass.ExampleFunction(out s);
However, none of them works. The only way I found to make it work was by initializing s.
ExampleStruct s = new ExampleStruct();
DllCass.ExampleFunction(s);
I have managed to fix that by rewriting the first snippet to the following code, but that feels kinda redundant.
internal static class DllClass {
[DllImport("Example.dll", EntryPoint = "ExampleFunction")]
public static extern int ExampleFunction([Out] out ExampleClass param);
}
I've read What's the difference between [Out] and out in C#? and because the accepted answer states that [Out]
and out
are equivalent in the context, it left me wondering why it didn't work for me and if my "solution" is appropriate.
Should I use both? Should I use only out
? Should I use only [Out]
?