Skip to content

Commit

Permalink
Added more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Apr 20, 2022
1 parent 92de45e commit 3c0a8e7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The library works best for a small to medium amount of strings (not multiple 100
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.

## Documentation
A more detailed documentation can be found [here](https://linkdotnet.github.io/StringBuilder/).
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.

## Benchmark

Expand Down
42 changes: 42 additions & 0 deletions docs/site/articles/pass_to_method.md
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 `.
2 changes: 2 additions & 0 deletions docs/site/articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
items:
- name: How does it work?
href: concepts.md
- name: Passing the ValueStringBuilder to a method
href: pass_to_method.md
- name: Comparison
href: comparison.md
- name: Known limitations
Expand Down

0 comments on commit 3c0a8e7

Please sign in to comment.