Skip to content

Commit 92b3191

Browse files
authored
Interpolated string handlers should avoid ref struct (#44123)
* Interpolated string handlers should avoid ref struct * Drop that they can't implement interfaces
1 parent ed03b2e commit 92b3191

File tree

4 files changed

+4
-4
lines changed

4 files changed

+4
-4
lines changed

docs/csharp/advanced-topics/performance/interpolated-string-handler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Finally, notice that the last warning doesn't invoke the interpolated string han
9393

9494
> [!IMPORTANT]
9595
>
96-
> The version of the `Logger` for this section is a `ref struct`. A `ref struct` minimizes allocations because it must be stored on the stack. However, `ref struct` types, in general, can't implement interfaces. This can cause compatibility issues for unit test frameworks and mocking types that use `ref struct` types for implementation.
96+
> Use `ref struct` for interpolated string handlers only if absolutely necessary. Using `ref struct` will have limitations as they must be stored on the stack. For example, they will not work if an interpolated string hole contains an `await` expression because the compiler will need to store the handler in the compiler-generated `IAsyncStateMachine` implementation.
9797
9898
## Add more capabilities to the handler
9999

docs/csharp/advanced-topics/performance/snippets/interpolated-string-handler/Logger-v2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace interpolated_string_handler.Version2
55
{
66
// <CoreInterpolatedStringHandler>
77
[InterpolatedStringHandler]
8-
public ref struct LogInterpolatedStringHandler
8+
public struct LogInterpolatedStringHandler
99
{
1010
// Storage for the built-up string
1111
StringBuilder builder;

docs/csharp/advanced-topics/performance/snippets/interpolated-string-handler/logger-v3.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace interpolated_string_handler.Version3
55
{
66
[InterpolatedStringHandler]
7-
public ref struct LogInterpolatedStringHandler
7+
public struct LogInterpolatedStringHandler
88
{
99
// Storage for the built-up string
1010
StringBuilder builder;

docs/csharp/advanced-topics/performance/snippets/interpolated-string-handler/logger-v4.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace interpolated_string_handler.Version4
55
{
66
[InterpolatedStringHandler]
7-
public ref struct LogInterpolatedStringHandler
7+
public struct LogInterpolatedStringHandler
88
{
99
// Storage for the built-up string
1010
StringBuilder builder;

0 commit comments

Comments
 (0)