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
Character constants vs String literals in C#
In C#, both character constants and string literals are used to represent text data, but they serve different purposes and have distinct syntax rules. Character constants represent single characters, while string literals represent sequences of characters (text).
Character Constants
Character constants are enclosed in single quotes and represent a single character. They are stored in variables of type char.
Syntax
char variableName = 'character';
Character constants can be −
Plain characters −
'x','A','5'Escape sequences −
',
''\t','\'Unicode characters −
'\u0041'(represents 'A')
Example
using System;
class Program {
static void Main() {
char letter = 'A';
char digit = '5';
char newline = '
';
char tab = '\t';
char unicode = '\u0041';
Console.WriteLine("Letter: " + letter);
Console.WriteLine("Digit: " + digit);
Console.WriteLine("Tab example:" + tab + "Indented text");
Console.WriteLine("Unicode A: " + unicode);
Console.WriteLine("New line example:" + newline + "Next line");
}
}
The output of the above code is −
Letter: A Digit: 5 Tab example: Indented text Unicode A: A New line example: Next line
String Literals
String literals are enclosed in double quotes and represent sequences of characters. They are stored in variables of type string.
Syntax
string variableName = "text"; string verbatimString = @"text with special formatting";
String literals can be −
Regular strings −
"Hello World"Strings with escape sequences −
"Line 1\nLine 2"Verbatim strings −
@"C:\Users\Name"
Example
using System;
class Program {
static void Main() {
string regularString = "Hello World";
string stringWithEscape = "Welcome\tto the\nwebsite";
string verbatimString = @"C:\Users\Documents\file.txt";
string multilineVerbatim = @"Line 1
Line 2
Line 3";
Console.WriteLine("Regular: " + regularString);
Console.WriteLine("With escape sequences:");
Console.WriteLine(stringWithEscape);
Console.WriteLine("Verbatim path: " + verbatimString);
Console.WriteLine("Multiline verbatim:");
Console.WriteLine(multilineVerbatim);
}
}
The output of the above code is −
Regular: Hello World With escape sequences: Welcome to the website Verbatim path: C:\Users\Documents\file.txt Multiline verbatim: Line 1 Line 2 Line 3
Comparison
| Feature | Character Constants | String Literals |
|---|---|---|
| Syntax | Single quotes: 'A'
|
Double quotes: "Hello"
|
| Data Type | char |
string |
| Size | Single character (2 bytes) | Multiple characters |
| Escape Sequences | Supported: '
|
Supported: "Line\nBreak"
|
| Verbatim Form | Not supported | Supported: @"text"
|
Common Escape Sequences
Example
using System;
class Program {
static void Main() {
Console.WriteLine("Common escape sequences:");
Console.WriteLine("Newline: First line\nSecond line");
Console.WriteLine("Tab: Column1\tColumn2");
Console.WriteLine("Backslash: C:\Program Files");
Console.WriteLine("Quote: She said "Hello"");
Console.WriteLine("Single quote: It's working");
char singleChar = ''';
Console.WriteLine("Single quote char: " + singleChar);
}
}
The output of the above code is −
Common escape sequences: Newline: First line Second line Tab: Column1 Column2 Backslash: C:\Program Files\ Quote: She said "Hello" Single quote: It's working Single quote char: '
Conclusion
Character constants use single quotes to represent single characters of type char, while string literals use double quotes to represent text sequences of type string. Both support escape sequences, but only strings support verbatim syntax with the @ prefix for easier handling of special characters.
