Search code examples
c#com-interop

Usage of [in, out] in methods


I want to ask if there is some difference between

public int Method1([In, Out] byte[] buffer);

and

public int Method2(byte[] buffer);

I came across http://referencesource.microsoft.com/#mscorlib/system/io/stream.cs,739 and wonder why [In, Out] is there?


Solution

  • Yes. There are differences.

    • public int Method2(byte[] buffer); uses implicit In:

      Indicates that data should be marshaled from the caller to the callee, but not back to the caller.

    • public int Method2(out byte[] buffer); uses Out:

      Indicates that data should be marshaled from callee back to caller.

    • public int Method2(ref byte[] buffer); uses [In, Out].