Skip to content

Commit 3c0a8e7

Browse files
committed
Added more documentation
1 parent 92de45e commit 3c0a8e7

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The library works best for a small to medium amount of strings (not multiple 100
3333
The normal use case is to add concatenate strings in a hot-path where the goal is to put as minimal pressure on the GC as possible.
3434

3535
## Documentation
36-
A more detailed documentation can be found [here](https://linkdotnet.github.io/StringBuilder/).
36+
A more detailed documentation can be found [here](https://linkdotnet.github.io/StringBuilder/). It is really important to understand how the `ValueStringBuilder` works so that you not run into weird situations where performance / allocations can even rise.
3737

3838
## Benchmark
3939

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
uid: pass_to_method
3+
---
4+
5+
# Passing the `ValueStringBuilder` to a method
6+
7+
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.
8+
9+
10+
```csharp
11+
public void MyFunction()
12+
{
13+
var stringBuilder = new ValueStringBuilder();
14+
stringBuilder.Append("Hello ");
15+
AppendMore(ref stringBuilder);
16+
}
17+
18+
private void AppendMore(ref ValueStringBuilder builder)
19+
{
20+
builder.Append("World");
21+
}
22+
```
23+
24+
This will print: `Hello World`
25+
26+
> :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.
27+
28+
```csharp
29+
public void MyFunction()
30+
{
31+
var stringBuilder = new ValueStringBuilder();
32+
stringBuilder.Append("Hello ");
33+
AppendMore(stringBuilder);
34+
}
35+
36+
private void AppendMore(ValueStringBuilder builder)
37+
{
38+
builder.Append("World");
39+
}
40+
```
41+
42+
This will print: `Hello `.

docs/site/articles/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
items:
44
- name: How does it work?
55
href: concepts.md
6+
- name: Passing the ValueStringBuilder to a method
7+
href: pass_to_method.md
68
- name: Comparison
79
href: comparison.md
810
- name: Known limitations

0 commit comments

Comments
 (0)