0
0
C Sharp (C#)programming~5 mins

Verbatim and raw string literals in C Sharp (C#)

Choose your learning style9 modes available
Introduction
Verbatim and raw string literals help you write text exactly as you want it, including new lines and special characters, without extra symbols or escapes.
When you want to write a file path without doubling backslashes, like C:\Users\Name.
When you need to include multi-line text exactly as it appears, such as a poem or a message.
When writing code that contains many quotes or special characters, and you want to avoid escaping them.
When you want to improve readability of strings that span multiple lines.
Syntax
C Sharp (C#)
Verbatim string literal:
string path = @"C:\Users\Name";

Raw string literal (C# 11+):
string text = """
This is a raw
multi-line string
with ""quotes"" and \backslashes\.
""";
Verbatim strings start with @ and use double quotes. Backslashes are treated as normal characters.
Raw string literals use triple quotes """ and preserve all formatting inside, including new lines and quotes.
Examples
Verbatim string lets you write Windows paths without doubling backslashes.
C Sharp (C#)
string filePath = @"C:\Program Files\App";
Verbatim strings keep new lines as written, so you can write multi-line text.
C Sharp (C#)
string multiLine = @"Line 1
Line 2";
Raw string literals keep all formatting and special characters exactly as typed.
C Sharp (C#)
string rawText = """
Hello ""World""!
Path: C:\Users\Name
""";
Sample Program
This program shows how verbatim and raw string literals print text exactly as written, including backslashes and quotes.
C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        string verbatim = @"C:\Users\Alice\Documents";
        string raw = """
This is a raw string literal.
It can contain ""quotes"" and \backslashes\ without escapes.
""";

        Console.WriteLine("Verbatim string output:");
        Console.WriteLine(verbatim);
        Console.WriteLine();
        Console.WriteLine("Raw string literal output:");
        Console.WriteLine(raw);
    }
}
OutputSuccess
Important Notes
Verbatim strings still require double quotes inside to be doubled, like "" for a double quote.
Raw string literals were introduced in C# 11, so make sure your project supports it.
Raw strings can use more than three quotes if your text contains triple quotes inside.
Summary
Verbatim strings start with @ and make writing paths and multi-line strings easier.
Raw string literals use triple quotes and preserve all formatting exactly as typed.
Both help avoid confusing escape sequences and improve code readability.