Skip to content

Commit 5711c4e

Browse files
authored
Merge pull request #8611 from SetTrend/main
Description added for C# option "Don't put ref or out on custom struct"
2 parents 90ac352 + c9a08a0 commit 5711c4e

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

docs/ide/reference/options-text-editor-csharp-advanced.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Options, Text Editor, C#, Advanced
33
description: Learn how to use the Advanced page in the C# section to modify the settings for editor formatting, code refactoring, and XML documentation comments for C#.
44
ms.custom: SEO-VS-2020
5-
ms.date: 06/01/2021
5+
ms.date: 10/29/2022
66
ms.topic: reference
77
f1_keywords:
88
- VS.ToolsOptionsPages.Text_Editor.CSharp.Outlining
@@ -149,6 +149,76 @@ Select these check boxes to display dotted vertical lines between the curly brac
149149

150150
When selected, inserts the XML elements for XML documentation comments after you type the `///` comment introduction. For more information about XML documentation, see [XML Documentation Comments (C# Programming Guide)](/dotnet/csharp/programming-guide/xmldoc/).
151151

152+
## Extract Method
153+
154+
- Don't put ref or out on custom struct
155+
156+
Uncheck this checkbox to avoid potentionally unintended cloning of structs by refencing existing struct objects when extracting an expression into a method call.
157+
158+
### Example
159+
160+
Supposed the following `class` and `struct` exist in your code:
161+
162+
```csharp
163+
public struct CustomStruct
164+
{
165+
private int Count;
166+
167+
public int Bump => ++Count;
168+
}
169+
170+
171+
public class CustomClass
172+
{
173+
public void DoIt()
174+
{
175+
CustomStruct cs = new CustomStruct();
176+
int i = 0;
177+
178+
i += cs.Bump; // select this line
179+
}
180+
}
181+
```
182+
183+
If the "Don't put ref or out on custom struct" option is **unchecked**, then the "Extract method" feature generates the following:
184+
185+
```C#
186+
public class CustomClass
187+
{
188+
public void DoIt()
189+
{
190+
CustomStruct cs = new CustomStruct();
191+
int i = 0;
192+
193+
NewMethod(ref cs, ref i);
194+
}
195+
196+
private static void NewMethod(ref CustomStruct cs, ref int i)
197+
=> i += cs.Bump;
198+
}
199+
```
200+
201+
If the "Don't put ref or out on custom struct" option is **checked**, then the "Extract method" feature generates the following:
202+
203+
```C#
204+
public class CustomClass
205+
{
206+
public void DoIt()
207+
{
208+
CustomStruct cs = new CustomStruct();
209+
int i = 0;
210+
211+
i = NewMethod(cs, i);
212+
}
213+
214+
private static int NewMethod(CustomStruct cs, int i)
215+
{
216+
i += cs.Bump;
217+
return i;
218+
}
219+
}
220+
```
221+
152222
## Inline Hints
153223

154224
- Inline Parameter Name Hints

0 commit comments

Comments
 (0)