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
String Literal Vs String Object in C#
In C#, strings can be created using string literals or as string objects. Understanding the difference between these two approaches is important for memory management and performance optimization.
String Literals
String literals are constants enclosed in double quotes "" or prefixed with @"" for verbatim strings. They are stored in a special area of memory called the string pool, where identical string values are shared to save memory.
Syntax
Following is the syntax for string literals −
string variableName = "string value"; string verbatimString = @"string with multiple lines or special characters";
Example
using System;
class Program {
static void Main(string[] args) {
// Regular string literal
string str1 = "Hello, World";
Console.WriteLine(str1);
// Verbatim string literal (multi-line)
string str2 = @"Welcome,
Hope you are doing great!";
Console.WriteLine(str2);
// String with escape sequences
string str3 = "Path: C:\Users\Documents";
Console.WriteLine(str3);
}
}
The output of the above code is −
Hello, World Welcome, Hope you are doing great! Path: C:\Users\Documents
String Objects
String objects can be created using various methods. Unlike string literals, each string object creation may result in a new memory allocation unless the compiler optimizes it.
Methods to Create String Objects
-
By assigning a string literal to a String variable
-
By using a String class constructor
-
By using the string concatenation operator (+)
-
By retrieving a property or calling a method that returns a string
-
By calling a formatting method to convert a value or object to its string representation
Example
using System;
class Program {
static void Main(string[] args) {
// Method 1: String literal assignment
string str1 = "John";
// Method 2: String constructor
string str2 = new string("Andy".ToCharArray());
// Method 3: String concatenation
string str3 = "Hello" + " " + "World";
// Method 4: String formatting
string str4 = String.Format("Age: {0}", 25);
Console.WriteLine("str1: " + str1);
Console.WriteLine("str2: " + str2);
Console.WriteLine("str3: " + str3);
Console.WriteLine("str4: " + str4);
// String comparison
if (String.Compare(str1, str2) == 0) {
Console.WriteLine(str1 + " and " + str2 + " are equal strings.");
} else {
Console.WriteLine(str1 + " and " + str2 + " are not equal strings.");
}
}
}
The output of the above code is −
str1: John str2: Andy str3: Hello World str4: Age: 25 John and Andy are not equal strings.
String Literal vs String Object Comparison
| String Literal | String Object |
|---|---|
| Stored in string pool for memory efficiency | May create new memory allocation each time |
| Identical literals share the same memory location | Each object may have separate memory location |
| Faster performance due to string interning | Slightly slower due to potential memory allocation |
| Created at compile time | Can be created at runtime |
Memory Behavior Example
using System;
class Program {
static void Main(string[] args) {
// String literals - same reference
string literal1 = "Hello";
string literal2 = "Hello";
// String objects - different references
string object1 = new string("Hello".ToCharArray());
string object2 = new string("Hello".ToCharArray());
// Reference comparison
Console.WriteLine("literal1 == literal2: " + ReferenceEquals(literal1, literal2));
Console.WriteLine("object1 == object2: " + ReferenceEquals(object1, object2));
// Value comparison
Console.WriteLine("literal1.Equals(object1): " + literal1.Equals(object1));
}
}
The output of the above code is −
literal1 == literal2: True object1 == object2: False literal1.Equals(object1): True
Conclusion
String literals are stored in the string pool and provide better memory efficiency, while string objects may create new memory allocations. For most scenarios, use string literals for better performance, and reserve string objects for dynamic string creation at runtime.
