Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are string literals in C#?
String literals in C# are constant string values enclosed in double quotes "" or prefixed with @"" for verbatim strings. A string literal contains characters that can include plain text, escape sequences, and Unicode characters.
Types of String Literals
C# supports several types of string literals −
Regular String Literals: Enclosed in double quotes with escape sequences
Verbatim String Literals: Prefixed with
@symbol, allowing multi-line strings and literal backslashesRaw String Literals: (C# 11+) Use triple quotes
"""for complex strings
Syntax
Following are the different syntaxes for string literals −
string regularString = "Hello World"; string verbatimString = @"C:\Users\Name\Documents"; string multiLineString = @"Line 1 Line 2 Line 3";
Regular String Literals
Regular string literals use escape sequences for special characters −
Example
using System;
class Program {
static void Main(string[] args) {
string greeting = "Hello, World!";
string withEscape = "She said, "Welcome to C#"";
string newLine = "First Line\nSecond Line";
string tab = "Name:\tJohn";
Console.WriteLine(greeting);
Console.WriteLine(withEscape);
Console.WriteLine(newLine);
Console.WriteLine(tab);
}
}
The output of the above code is −
Hello, World! She said, "Welcome to C#" First Line Second Line Name: John
Verbatim String Literals
Verbatim strings are prefixed with @ and treat backslashes as literal characters, making them ideal for file paths and multi-line strings −
Example
using System;
class Program {
static void Main(string[] args) {
string filePath = @"C:\Users\Documents\file.txt";
string multiLine = @"Welcome to C#,
Hope you are doing great!
Happy coding!";
string withQuotes = @"He said, ""Hello there!""";
Console.WriteLine("File Path: " + filePath);
Console.WriteLine(multiLine);
Console.WriteLine(withQuotes);
}
}
The output of the above code is −
File Path: C:\Users\Documents\file.txt Welcome to C#, Hope you are doing great! Happy coding! He said, "Hello there!"
Common Escape Sequences
| Escape Sequence | Description | Example |
|---|---|---|
" |
Double quote | "He said "Hi"" |
\ |
Backslash | "Path\file.txt" |
|
New line | "Line1\nLine2" |
\t |
Tab | "Name:\tValue" |
String Literals vs Variables
Example
using System;
class Program {
static void Main(string[] args) {
// String literals
Console.WriteLine("This is a string literal");
Console.WriteLine(@"Verbatim string literal");
// String variables
string message = "Hello";
string name = "Alice";
string combined = message + ", " + name + "!";
Console.WriteLine(combined);
// String interpolation with literals
Console.WriteLine($"Welcome, {name}!");
}
}
The output of the above code is −
This is a string literal Verbatim string literal Hello, Alice! Welcome, Alice!
Conclusion
String literals in C# provide flexible ways to define constant string values using regular quotes for simple strings, verbatim strings with @ for paths and multi-line content, and various escape sequences for special characters. Understanding these different formats helps write cleaner and more readable code.
