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

String creation and literal types in C Sharp (C#)

Choose your learning style9 modes available
Introduction
Strings let us store and use words or sentences in our programs. Literal types help us write these words directly in the code.
When you want to store a name or message in your program.
When you need to show text to the user.
When you want to combine words or sentences.
When you want to write a fixed phrase or word directly in your code.
When you want to create a string that includes special characters like new lines.
Syntax
C Sharp (C#)
string variableName = "your text here";
string verbatimString = @"your text here";
Use double quotes " " to create a normal string.
Use @ before quotes to create a verbatim string that keeps spaces and new lines exactly.
Examples
This creates a simple string with the text Hello, world!
C Sharp (C#)
string greeting = "Hello, world!";
This verbatim string lets you write file paths easily without doubling backslashes.
C Sharp (C#)
string path = @"C:\Users\Name\Documents";
This verbatim string keeps the new lines as part of the text.
C Sharp (C#)
string multiline = @"Line 1
Line 2
Line 3";
Use backslash \ to include double quotes inside a normal string.
C Sharp (C#)
string quote = "She said, \"Hello!\"";
Sample Program
This program shows two ways to create strings and prints them. The verbatim string keeps the new line and quotes as typed.
C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        string normal = "Hello, friend!";
        string verbatim = @"This is a verbatim string
It keeps new lines and ""quotes"".";
        Console.WriteLine(normal);
        Console.WriteLine(verbatim);
    }
}
OutputSuccess
Important Notes
Normal strings use escape sequences like \n for new line or \" for quotes.
Verbatim strings start with @ and do not process escape sequences except doubling quotes "".
Use verbatim strings for file paths or multi-line text to make code easier to read.
Summary
Strings store text using double quotes.
Verbatim strings start with @ and keep text exactly as typed.
Escape sequences let you add special characters inside normal strings.