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
Default value of StringBuilder in C#
The default operator in C# returns the default value for any data type. For reference types like StringBuilder, the default value is null. This is useful when you need to initialize a StringBuilder variable without creating an instance immediately.
Syntax
Following is the syntax for using the default operator with StringBuilder −
StringBuilder variable = default(StringBuilder);
In C# 7.1 and later, you can also use the simplified syntax −
StringBuilder variable = default;
Using default(StringBuilder)
Example
using System;
using System.Text;
public class Demo {
public static void Main() {
StringBuilder str = default(StringBuilder);
Console.WriteLine("Default for StringBuilder = " + str);
if (str == null) {
Console.WriteLine("StringBuilder is null");
}
}
}
The output of the above code is −
Default for StringBuilder = StringBuilder is null
Comparing Default Values
Example
using System;
using System.Text;
public class Program {
public static void Main() {
StringBuilder str1 = default(StringBuilder);
StringBuilder str2 = default;
StringBuilder str3 = null;
Console.WriteLine("str1 == null: " + (str1 == null));
Console.WriteLine("str2 == null: " + (str2 == null));
Console.WriteLine("str1 == str2: " + (str1 == str2));
Console.WriteLine("str1 == str3: " + (str1 == str3));
// Initialize a StringBuilder properly
StringBuilder str4 = new StringBuilder("Hello");
Console.WriteLine("str4 content: " + str4);
}
}
The output of the above code is −
str1 == null: True str2 == null: True str1 == str2: True str1 == str3: True str4 content: Hello
Default Values for Different Types
| Type Category | Example Type | Default Value |
|---|---|---|
| Reference Types | StringBuilder, string, object | null |
| Value Types | int, double, bool | 0, 0.0, false |
| Nullable Value Types | int?, bool? | null |
Practical Usage
The default operator is commonly used in generic methods and when you need to reset variables to their initial state −
Example
using System;
using System.Text;
public class StringBuilderManager {
private StringBuilder buffer;
public void InitializeBuffer() {
buffer = new StringBuilder("Initial content");
Console.WriteLine("Buffer initialized: " + buffer);
}
public void ResetBuffer() {
buffer = default(StringBuilder);
Console.WriteLine("Buffer reset to default: " + (buffer == null ? "null" : buffer.ToString()));
}
public static void Main() {
StringBuilderManager manager = new StringBuilderManager();
manager.InitializeBuffer();
manager.ResetBuffer();
}
}
The output of the above code is −
Buffer initialized: Initial content Buffer reset to default: null
Conclusion
The default operator returns null for StringBuilder since it's a reference type. This is useful for initialization and resetting variables, especially in generic programming scenarios where you need the default value of any type.
