C# and .NET • Modern C# baseline
Strings, Formatting, and Text
Build readable strings with interpolation and distinguish text formatting from data serialization.
C# strings are immutable Unicode text values. Interpolation is convenient for user-facing messages, while invariant formatting can be important for machine-readable numeric text. Do not construct JSON, SQL, or shell commands by concatenating strings; use serializers or parameterized APIs.
Key points
- Repeated concatenation in a loop can allocate many intermediate strings.
- Use StringBuilder for measured high-volume incremental construction.
Example
using System.Globalization;
decimal amount = 1250.5m;
string display = amount.ToString("C", CultureInfo.GetCultureInfo("en-US"));
Console.WriteLine(display);
What to expect
$1,250.50
Practice exercise
Format the same amount using a different culture and note why display formatting differs from stored data.
Key idea: Formatting is presentation; preserve typed values until the final output boundary.