String Creation and Assignment - Building and Managing Strings in C#
Vaibhav • September 10, 2025
Strings are the backbone of most C# programs. Whether you’re displaying messages, processing user input, or building file paths, you’ll constantly create, assign, and manipulate strings. This article explores all the ways to create strings, assign them, and understand what happens under the hood. We’ll cover literals, verbatim and interpolated strings, concatenation, copying, joining, and best practices for safe, efficient string handling.
This article builds on the fundamentals of strings and immutability. You’ll see how different creation methods work, how assignment interacts with memory, and how to avoid common mistakes that can lead to bugs or performance issues.
String literals - the simplest way
The most common way to create a string is with a literal, using double quotes. String literals are used for fixed text, prompts, and messages.
string hello = "Hello, world!";
string empty = "";
string path = "C:\\Users\\Vaibhav\\Documents"; // escape backslashes
string quote = "She said, \"Welcome!\""; // escape double quotes
- Use
""
for an empty string. - Escape special characters (like
\\
for backslash,\n
for newline,\"
for double quote). - String literals are stored in memory as Unicode, supporting international text.
Verbatim strings - for file paths and multi-line text
Use @""
to create verbatim strings, which treat backslashes and newlines
literally. This is especially useful for Windows file paths and multi-line text blocks.
string folder = @"C:\Projects\CSharp";
string multiLine = @"First line
Second line
Third line";
string quote = @"She said, ""Welcome!"""; // double quotes are escaped by doubling them
- No need to escape backslashes in verbatim strings.
- To include a double quote, use two double quotes
""
. - Great for file paths, XML, JSON, or any text with lots of special characters.
String concatenation - combining strings
You can join strings using the +
operator. This is useful for building up
messages or combining variables and literals.
string first = "Hello";
string second = "Vaibhav";
string message = first + ", " + second + "!"; // "Hello, Vaibhav!"
string repeated = first + first + first; // "HelloHelloHello"
- Concatenation creates a new string every time (strings are immutable).
- For many joins in a loop, prefer
StringBuilder
for better performance (covered later). - Concatenation works with variables, literals, and even expressions.
String interpolation - modern, readable formatting
C# supports string interpolation with $""
syntax. This lets you embed variables
and expressions directly inside a string.
string name = "Vaibhav";
int score = 95;
string result = $"Name: {name}, Score: {score}"; // "Name: Vaibhav, Score: 95"
string math = $"2 + 2 = {2 + 2}"; // "2 + 2 = 4"
- Place variables or expressions inside
{ }
braces. - Interpolation is safer and easier to read than concatenation for complex strings.
- You can use formatting inside braces, e.g.
{score:D3}
for leading zeros.
Copying and assigning strings
Assigning one string variable to another copies the reference, not the contents. Because strings are immutable, this is safe and efficient.
string original = "CSharp";
string copy = original; // both refer to the same string object
copy = "DotNet"; // copy now refers to a new string; original is unchanged
- Strings behave like value types for assignment, but are actually reference types.
- Changing
copy
does not affectoriginal
. - Comparing two strings with
==
checks their contents, not their references.
Creating strings from arrays and collections
You can build strings from arrays of characters or join collections of strings. This is useful for dynamic or computed text.
// From char array
char[] letters = { 'C', '#', '!' };
string word = new string(letters); // "C#!"
// Joining a list of strings
var names = new List<string> { "Ada", "Grace", "Linus" };
string allNames = string.Join(", ", names); // "Ada, Grace, Linus"
// Joining with a different separator
string csv = string.Join(";", names); // "Ada;Grace;Linus"
string.Join
is ideal for combining many strings with a separator.- You can join arrays, lists, or any collection that implements
IEnumerable<string>
.
Reading strings from user input
Use Console.ReadLine()
to get a string from the user. This is the standard way
to read input in console apps.
Console.WriteLine("Enter your name:");
string userName = Console.ReadLine();
if (!string.IsNullOrEmpty(userName))
Console.WriteLine($"Hello, {userName}!");
else
Console.WriteLine("No name entered.");
- Always check for
null
or empty strings when reading user input.
Advanced: Creating strings with repeated characters
Sometimes you need a string of repeated characters (e.g., for padding or formatting). Use the string constructor:
string dashes = new string('-', 10); // "----------"
string stars = new string('*', 5); // "*****"
Common mistakes and best practices
- Don’t use
==
for reference comparison - In C#,==
compares string contents, not references. This is usually what you want. - Don’t build large strings with
+
in loops - UseStringBuilder
for efficiency (covered later). - Always check for
null
or empty strings before using user input. - Use interpolation for clarity - It’s safer and easier to read than concatenation.
- Prefer
@""
for file paths and multi-line text - It reduces escaping and improves readability.
Prefer $""
interpolation for most string
formatting. Use string.Join
for combining lists, and StringBuilder
for heavy modifications or loops.
Summary
Strings in C# can be created in many ways: literals, verbatim and interpolated strings, concatenation, joining arrays or lists, and reading user input. Assignment is safe and predictable thanks to immutability. By using the right creation and assignment patterns, you’ll write clearer, safer, and more efficient code. In the next article, we’ll explore string concatenation in depth-including performance tips, pitfalls, and alternatives for building large or complex strings.