A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1417 below:

CA1417: Do not use OutAttribute on string parameters for P/Invokes (code analysis) - .NET

CA1417: Do not use OutAttribute on string parameters for P/Invokes

In this article Property Value Rule ID CA1417 Title Do not use OutAttribute on string parameters for P/Invokes Category Interoperability Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As warning Cause

A P/Invoke string parameter is passed by value and marked with OutAttribute.

Rule description

The .NET runtime automatically performs string interning. If an interned string marked with OutAttribute is passed by value to a P/Invoke, the runtime can be destabilized.

How to fix violations

If marshalling of modified string data back to the caller is required, pass the string by reference instead. Otherwise, the OutAttribute can be removed without any other changes.

 // Violation
[DllImport("MyLibrary")]
private static extern void Foo([Out] string s);

// Fixed: passed by reference
[DllImport("MyLibrary")]
private static extern void Foo(out string s);

// Fixed: marshalling data back to caller is not required
[DllImport("MyLibrary")]
private static extern void Foo(string s);
When to suppress warnings

It is not safe to suppress a warning from this rule.

See also

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.

Additional resources

In this article

Was this page helpful?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4