Skip to content

Commit d72385a

Browse files
authored
Add note about nullable value types (#11983)
* Add note about nullable value types Fixes #11834 * Update docs/csharp/write-safe-efficient-code.md Co-Authored-By: BillWagner <[email protected]>
1 parent bf0a88e commit d72385a

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

docs/csharp/write-safe-efficient-code.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ The compiler generates more efficient code when you call members of a
233233
is always an `in` parameter passed by reference to the member method. This optimization
234234
saves copying when you use a `readonly struct` as an `in` argument.
235235

236+
You should not pass a nullable value type as an `in` argument. The <xref:System.Nullable%601> type is not declared as a read-only struct. That means the compiler must generate defensive copies for any nullable value type argument passed to a method using the `in` modifier on the parameter declaration.
237+
236238
You can see an example program that demonstrates the performance differences using [Benchmark.net](https://www.nuget.org/packages/BenchmarkDotNet/) in our [samples repository](https://github.com/dotnet/samples/tree/master/csharp/safe-efficient-code/benchmark) on GitHub. It compares passing a mutable struct by value and by reference with passing an immutable struct by value and by reference. The use of the immutable struct and pass by reference is fastest.
237239

238240
## Use `ref struct` types to work with blocks or memory on a single stack frame

0 commit comments

Comments
 (0)