Practical coding tips, real solutions and career insights for software developers — straight to your inbox. No fluff.
No spam. Unsubscribe anytime. Privacy policy
Thanks for subscribing. First issue lands soon.
String interpolation, introduced in C# 6.0, provides a more readable and concise way to format strings compared to traditional concatenation (+) or string.Format(). Instead of manually inserting variables or placeholders, you can use the $ symbol before a string to directly embed expressions inside brackets.
string name = "Walt"; string job = 'Software Engineer'; string message = $"Hello, my name is {name} and I am a {job}"; Console.WriteLine(message);
This would produce the final output of:
Hello, my name is Walt and I am a Software Engineer
String interpolation can also be chained together into a multiline string (@) for even cleaner more concise results:
string name = "Walt"; string html = $@" <div> <h1>Welcome, {name}!</h1> </div>";
In C#, you can format an integer with commas (thousands separator) using ToString with a format specifier.
int number = 1234567; string formattedNumber = number.ToString("N0"); // "1,234,567" Console.WriteLine(formattedNumber);
Explanation:
"N0": The "N" format specifier stands for Number, and "0" means no decimal places. The output depends on the culture settings, so in regions where , is the decimal separator, you might get 1.234.567.
Alternative:
You can also specify culture explicitly if you need a specific format:
using System.Globalization; int number = 1234567; string formattedNumber = number.ToString("N0", CultureInfo.InvariantCulture); Console.WriteLine(formattedNumber); // "1,234,567"
When working with financial data in C#, proper currency formatting is essential for clear and professional presentation. The .NET framework provides several convenient methods to format numeric values as currency, with the most common being the ToString() method with the "C" format specifier.
For example, decimal amount = 1234.56m; string formatted = amount.ToString("C"); will display "$1,234.56" in US culture.
decimal amount = 1234.56m; string formatted = amount.ToString("C");
For more control over the formatting, you can specify a culture explicitly using CultureInfo - amount.ToString("C", new CultureInfo("fr-FR")) would display "1 234,56 €".
amount.ToString("C", new CultureInfo("fr-FR"))
This allows your application to handle different currency symbols, decimal separators, and grouping conventions appropriately.
If you need to handle multiple currencies or require more specialized formatting, you can also use the String.Format() method or string interpolation with custom format strings.
For instance, String.Format("{0:C}", amount) or $"{amount:C}" achieves the same result as ToString("C"). Additionally, you can control the number of decimal places using format strings like "C2" for two decimal places.
String.Format("{0:C}", amount)
$"{amount:C}"
Remember that when dealing with financial calculations, it's best practice to use the decimal type rather than float or double to avoid rounding errors that could impact currency calculations.
Example
decimal price = 1234.56m; // Basic currency formatting Console.WriteLine(price.ToString("C")); // Output: $1,234.56 // Currency formatting with specific culture Console.WriteLine(price.ToString("C", new CultureInfo("de-DE"))); // Output: 1.234,56 € // Currency formatting with string interpolation Console.WriteLine($"{price:C}"); // Output: $1,234.56 // Controlling decimal places Console.WriteLine(price.ToString("C3")); // Output: $1,234.560
When working with relational databases, JOIN operations allow you to retrieve data from multiple tables based on a common column.
JOIN
SQL Server supports different types of joins, each serving a specific purpose. Let’s break them down with examples.
The INNER JOIN returns only the rows where there is a match in both tables.
INNER JOIN
SELECT A.id, A.name, B.order_id FROM Customers A INNER JOIN Orders B ON A.id = B.customer_id;
The LEFT JOIN returns all rows from the left table (Customers), and only matching rows from the right table (Orders). If there’s no match, NULL values are returned for the right table columns.
LEFT JOIN
Customers
Orders
SELECT A.id, A.name, B.order_id FROM Customers A LEFT JOIN Orders B ON A.id = B.customer_id;
order_id
The RIGHT JOIN works the opposite of LEFT JOIN, returning all rows from the right table (Orders) and only matching rows from the left table (Customers).
RIGHT JOIN
SELECT A.id, A.name, B.order_id FROM Customers A RIGHT JOIN Orders B ON A.id = B.customer_id;
name
The FULL JOIN returns all records from both tables. If there’s no match, NULL values will be shown in the missing columns.
FULL JOIN
SELECT A.id, A.name, B.order_id FROM Customers A FULL JOIN Orders B ON A.id = B.customer_id;
Understanding these joins can help you extract data efficiently and ensure that your queries return the expected results. Happy querying!
Register for my free weekly newsletter.