Welcome to another tutorial at The Coding College! Today, we’ll cover C# String Interpolation, a modern, clean, and efficient way to embed variables directly into strings.
Mastering string interpolation will improve your code readability and make string handling more enjoyable.
For more tutorials on C# and programming, visit thecodingcollege.com.
What is String Interpolation in C#?
String Interpolation in C# allows you to insert variables, expressions, or even method calls directly into a string.
It is denoted by the $
symbol placed before the string and uses curly braces {}
to embed values.
Syntax of String Interpolation
string result = $"Text {variable}";
$
: Indicates string interpolation.{}
: Placeholders to insert variables or expressions.
Example 1: Simple String Interpolation
Here is a basic example to display a full name using string interpolation:
using System;
class Program
{
static void Main()
{
string firstName = "John";
string lastName = "Doe";
// Using string interpolation
string fullName = $"Full Name: {firstName} {lastName}";
Console.WriteLine(fullName);
}
}
Output:
Full Name: John Doe
Example 2: Embedding Expressions
String interpolation allows you to include expressions like calculations directly inside the string.
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 20;
// Performing calculations inside the interpolated string
string result = $"The sum of {a} and {b} is {a + b}.";
Console.WriteLine(result);
}
}
Output:
The sum of 10 and 20 is 30.
Example 3: Formatting Dates and Numbers
You can format dates and numbers within an interpolated string.
using System;
class Program
{
static void Main()
{
DateTime today = DateTime.Now;
double price = 199.99;
// Formatting date and number
string message = $"Today's date is {today:MMMM dd, yyyy} and the price is {price:C}.";
Console.WriteLine(message);
}
}
Output:
Today's date is March 14, 2024 and the price is $199.99.
{today:MMMM dd, yyyy}
: Formats the date to a readable format.{price:C}
: Displays the price as currency.
Example 4: Combining Strings and Methods
You can even call methods directly within the string interpolation.
using System;
class Program
{
static void Main()
{
string name = "The Coding College";
// Using string interpolation with methods
string upperCase = $"Welcome to {name.ToUpper()}!";
Console.WriteLine(upperCase);
}
}
Output:
Welcome to THE CODING COLLEGE!
Advantages of String Interpolation
- Improved Readability: Makes code cleaner and easier to understand.
- Reduced Errors: Avoids manual string concatenation mistakes.
- Inline Expressions: You can perform calculations or call methods directly.
- No Placeholder Confusion: Unlike
String.Format
, there’s no need to match index positions.
String Interpolation vs String.Format
Feature | String Interpolation | String.Format |
---|---|---|
Syntax | $"Text {variable}" | String.Format("Text {0}", var) |
Readability | Easy and clean | Less readable |
Indexing | Not required | Index-based placeholders |
Performance | Slightly better | Slower for large operations |
Example 5: Multiline Interpolated Strings
You can use string interpolation with multi-line strings using verbatim strings (@
).
using System;
class Program
{
static void Main()
{
string title = "C# String Interpolation";
string author = "The Coding College";
string message = $@"
============================
Title: {title}
Author: {author}
Date: {DateTime.Now:MMMM dd, yyyy}
============================";
Console.WriteLine(message);
}
}
Output:
============================
Title: C# String Interpolation
Author: The Coding College
Date: March 14, 2024
============================
When to Use String Interpolation?
Use string interpolation:
- For cleaner and concise string formatting.
- When embedding variables or expressions directly into strings.
- For dynamic or real-time string generation.
Conclusion
String interpolation is a powerful feature in C# that simplifies string formatting and improves code readability.
Instead of juggling multiple concatenations or managing placeholder indexes, you can write cleaner, more modern code using the $
syntax.
For more C# tutorials and coding resources, visit The Coding College—your go-to destination for programming knowledge!