-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92de45e
commit 3c0a8e7
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
uid: pass_to_method | ||
--- | ||
|
||
# Passing the `ValueStringBuilder` to a method | ||
|
||
As the [ValueStringBuilder](xref:LinkDotNet.StringBuilder.ValueStringBuilder) is `ref struct` you should be careful when passing the instance around. You should pass the reference and not the instance. | ||
|
||
|
||
```csharp | ||
public void MyFunction() | ||
{ | ||
var stringBuilder = new ValueStringBuilder(); | ||
stringBuilder.Append("Hello "); | ||
AppendMore(ref stringBuilder); | ||
} | ||
|
||
private void AppendMore(ref ValueStringBuilder builder) | ||
{ | ||
builder.Append("World"); | ||
} | ||
``` | ||
|
||
This will print: `Hello World` | ||
|
||
> :warning: The following code snippet will show how it *does not* work. If the instance is passed not via reference but via value then first allocations will happen and second the end result is not what one would expect. | ||
```csharp | ||
public void MyFunction() | ||
{ | ||
var stringBuilder = new ValueStringBuilder(); | ||
stringBuilder.Append("Hello "); | ||
AppendMore(stringBuilder); | ||
} | ||
|
||
private void AppendMore(ValueStringBuilder builder) | ||
{ | ||
builder.Append("World"); | ||
} | ||
``` | ||
|
||
This will print: `Hello `. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters