Indicates that data should be marshaled from callee back to caller.
Namespace:
System.Runtime.InteropServices
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.Parameter, Inherited := False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class OutAttribute _
Inherits Attribute
Dim instance As OutAttribute
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class OutAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Parameter, Inherited = false)]
[ComVisibleAttribute(true)]
public ref class OutAttribute sealed : public Attribute
public final class OutAttribute extends Attribute
You can apply this attribute to parameters.
The OutAttribute is optional. The attribute is supported for COM interop and platform invoke only. In the absence of explicit settings, the interop marshaler assumes rules based on the parameter type, whether the parameter is passed by reference or by value, and whether the type is blittable or non-blittable. For example, the StringBuilder class is always assumed to be In/Out and an array of strings passed by value is assumed to be In.
Out-only behavior is never a default marshaling behavior for parameters. You can apply the OutAttribute to value and reference types passed by reference to change In/Out behavior to Out-only behavior, which is equivalent to using the out keyword in C#. For example, arrays passed by value, marshaled as In-only parameters by default, can be changed to Out-only. However, the behavior does not always provide expected semantics when the types include all-blittable elements or fields because the interop marshaler uses pinning. If you do not care about passing data into the callee, Out-only marshaling can provide better performance for non-blittable types.
Combining the InAttribute and OutAttribute is particularly useful when applied to arrays and formatted, non-blittable types. Callers see the changes a callee makes to these types only when you apply both attributes. Since these types require copying during marshaling, you can use InAttribute and OutAttribute to reduce unnecessary copies.
For more information on the effect of OutAttribute on marshaling behavior, see Directional Attributes.
The following example shows how to apply the InAttribute and OutAttribute to a platform invoke prototype that passes an array as a parameter. The combination of directional attributes allows the caller to see the changes made by the callee.
Imports System.Runtime.InteropServices
' Declare a class member for each structure element.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class OpenFileName
Public structSize As Integer = 0
Public filter As String = Nothing
Public file As String = Nothing
' ...
End Class 'OpenFileName
Public Class LibWrap
' Declare managed prototype for the unmanaged function.
Declare Unicode Function GetOpenFileName Lib "Comdlg32.dll" ( _
<[In](), Out()> ByVal ofn As OpenFileName) As Boolean
End Class 'LibWrap
Public Class App
Public Shared Sub Main()
End Sub 'Main
End Class 'App
using System.Runtime.InteropServices;
using System;
// Declare a class member for each structure element.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class OpenFileName
{
public int structSize = 0;
public string filter = null;
public string file = null;
// ...
}
public class LibWrap
{
// Declare a managed prototype for the unmanaged function.
[DllImport("Comdlg32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
}
public class MainMethod
{
static void Main()
{ }
}
using namespace System;
using namespace System::Runtime::InteropServices;
// Declare a class member for each structure element.
[StructLayout(LayoutKind::Sequential,CharSet=CharSet::Unicode)]
public ref class OpenFileName
{
public:
int structSize;
String^ filter;
String^ file;
// ...
};
public ref class LibWrap
{
public:
// Declare a managed prototype for the unmanaged function.
[DllImport("Comdlg32.dll",CharSet=CharSet::Unicode)]
static bool GetOpenFileName( [In,Out]OpenFileName^ ofn );
};
void main() {}
System..::.Object
System..::.Attribute
System.Runtime.InteropServices..::.OutAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Other Resources