Character and String Basics
Vaibhav • September 11, 2025
In programming, text is everywhere - from user names and messages to file paths and error logs. In C#, text is
represented using two fundamental types: char
and string
. This article introduces both, explains how they differ, and shows how to
work with them using only the concepts we've covered so far. We’ll also explore how to combine strings, format
them neatly, and handle special characters like newlines and tabs.
Understanding char
and string
A char
represents a single character - just one letter, digit, or symbol. Think
of it as the smallest unit of text. A string
, on the other hand, is a sequence
of characters. It can be empty, short, or very long. You’ve already seen strings in action when printing
messages with Console.WriteLine
.
char letter = 'A';
string greeting = "Hello";
In this example, letter
holds the character 'A'
, while greeting
holds the word "Hello"
. Notice the difference in quotation marks: char
uses single quotes (' '
), while string
uses double quotes (" "
).
A char
is a value type and occupies 2 bytes in
memory (because it uses Unicode). A string
is a reference type and can hold
any number of characters.
Displaying characters and strings
You can use Console.WriteLine
to display both characters and strings. This is
useful for debugging, logging, or simply showing output to the user.
char symbol = '#';
string name = "Vaibhav";
Console.WriteLine(symbol); // Output: #
Console.WriteLine(name); // Output: Vaibhav
Each call to Console.WriteLine
prints the value followed by a newline. The
character symbol
prints as a single glyph, while the string name
prints the full sequence of letters.
String concatenation - combining text
Concatenation means joining strings together. In C#, you can use the +
operator
to combine strings. This is one of the simplest ways to build messages or labels.
string firstName = "Vaibhav";
string lastName = "Lawand";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName); // Output: Vaibhav Lawand
Here, we combine firstName
, a space " "
, and
lastName
to form fullName
. The result is a
readable string with proper spacing.
Concatenation creates a new string each time. Strings in C# are immutable - once
created, they cannot be changed. Every +
operation produces a new string in
memory.
String interpolation - formatting with ease
Interpolation is a cleaner way to build strings. Instead of using +
, you embed
variables directly inside a string using the $
symbol and curly braces.
string city = "Pune";
string message = $"Welcome to {city}!";
Console.WriteLine(message); // Output: Welcome to Pune!
The $
before the string tells C# to treat it as an interpolated string. Inside
the curly braces, you can place any variable or expression. This makes the code easier to read and maintain.
Prefer string interpolation over concatenation for building readable messages. It reduces clutter and avoids mistakes with spacing or punctuation.
Escape characters - special formatting
Sometimes you need to include special characters in a string - like a newline or a tab. These are called
escape characters, and they start with a backslash (\
).
string formatted = "Name:\tVaibhav\nLocation:\tPune";
Console.WriteLine(formatted);
This string contains two escape sequences:
\t
adds a horizontal tab (like pressing the Tab key).\n
adds a newline (like pressing Enter).
The output will look like this:
Name: Vaibhav
Location: Pune
Escape characters help format output neatly, especially when displaying structured data.
You can include double quotes inside a string by escaping them: \"
. For
example: "He said, \"Hello!\""
.
Empty strings and whitespace
An empty string is a string with no characters. You can create one using ""
.
It’s different from null
, which means no value at all.
string empty = "";
Console.WriteLine(empty); // Output: (nothing)
You can also create strings that contain only spaces or tabs. These are not empty, but they may look blank when printed.
Combining characters into strings
You can build strings by combining multiple char
values. This is useful when
constructing text one character at a time.
char c1 = 'C';
char c2 = '#';
char c3 = '!';
string result = "" + c1 + c2 + c3;
Console.WriteLine(result); // Output: C#!
Here, we start with an empty string (""
) and add characters to it. The result
is a string that contains all three characters.
String length and indexing (preview)
While we won’t explore string methods yet, it’s helpful to know that strings have a Length
property that tells you how many characters they contain. You can also
access individual characters using indexing.
string word = "CSharp";
Console.WriteLine(word.Length); // Output: 6
Console.WriteLine(word[0]); // Output: C
This shows that word
has six characters, and the first character (at index 0)
is C
. We’ll explore this more deeply in future chapters.
Common mistakes and how to avoid them
Beginners often confuse char
and string
. For
example, writing char letter = "A";
will cause an error because "A"
is a string, not a character.
// Incorrect
char letter = "A"; // ❌
// Correct
char letter = 'A'; // ✅
Another mistake is forgetting escape characters. For example, writing "Hello"World"
will break the string. You need to escape the inner quotes: "Hello\"World\""
.
Use single quotes for char
and double quotes for string
. When building messages, prefer interpolation over concatenation for
clarity.
Summary
In this article, you learned the difference between char
and string
- the building blocks of text in C#. A char
holds a single character, while a string
holds a sequence. You saw how to display them, combine them using concatenation and interpolation, and format
them with escape characters like \n
and \t
. We
also previewed how strings can be indexed and measured.
These concepts are foundational. Whether you're printing a welcome message, formatting a report, or building a user interface, strings are everywhere. Mastering them early will make your code cleaner, clearer, and more expressive.