-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Closed
Labels
advanced-concepts/subsvcdotnet-csharp/svchelp wantedGood for community contributors to help [up-for-grabs]Good for community contributors to help [up-for-grabs]
Description
The following code causes boxing of the 'salary' value type.
double salary = 1200;
string text = $"Salary {salary:C2}";
Console.WriteLine(text);
Boxing is evil because it is “slow”. It creates a reference type (the boxed value) which the garbage collector has to clean up (which is also “slow”). You can easily do this by adding a ToString().
double salary = 1200;
string text = $"Salary {salary.ToString("C2")}";
Console.WriteLine(text);
I think this should be mentioned in the docs.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: d0665571-35b7-0841-5719-b69456b35cfb
- Version Independent ID: 0410420b-3b12-6d96-40cf-56f22ac19a1d
- Content: $ - string interpolation - C# reference
- Content Source: docs/csharp/language-reference/tokens/interpolated.md
- Product: dotnet-csharp
- Technology: csharp-language-reference
- GitHub Login: @pkulikov
- Microsoft Alias: wiwagn
a-a-k
Metadata
Metadata
Assignees
Labels
advanced-concepts/subsvcdotnet-csharp/svchelp wantedGood for community contributors to help [up-for-grabs]Good for community contributors to help [up-for-grabs]