From d122fe5a9da296059c762154ea8eb11e82006cda Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 23 Apr 2019 10:37:37 -0400 Subject: [PATCH 1/2] Add note about nullable value types Fixes #11834 --- docs/csharp/write-safe-efficient-code.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/csharp/write-safe-efficient-code.md b/docs/csharp/write-safe-efficient-code.md index de5883d7ed86e..219685ce23f73 100644 --- a/docs/csharp/write-safe-efficient-code.md +++ b/docs/csharp/write-safe-efficient-code.md @@ -233,6 +233,8 @@ The compiler generates more efficient code when you call members of a is always an `in` parameter passed by reference to the member method. This optimization saves copying when you use a `readonly struct` as an `in` argument. +You should not pass a nullable value type as an `in` argument. The type is not declared as a readonly 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. + 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. ## Use `ref struct` types to work with blocks or memory on a single stack frame From d1d79240338ea100f075c2f7c801994902c85473 Mon Sep 17 00:00:00 2001 From: Ron Petrusha Date: Thu, 25 Apr 2019 14:19:07 -0400 Subject: [PATCH 2/2] Update docs/csharp/write-safe-efficient-code.md Co-Authored-By: BillWagner --- docs/csharp/write-safe-efficient-code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/write-safe-efficient-code.md b/docs/csharp/write-safe-efficient-code.md index 219685ce23f73..aeba74ce4732e 100644 --- a/docs/csharp/write-safe-efficient-code.md +++ b/docs/csharp/write-safe-efficient-code.md @@ -233,7 +233,7 @@ The compiler generates more efficient code when you call members of a is always an `in` parameter passed by reference to the member method. This optimization saves copying when you use a `readonly struct` as an `in` argument. -You should not pass a nullable value type as an `in` argument. The type is not declared as a readonly 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. +You should not pass a nullable value type as an `in` argument. The 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. 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.