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 operator in C#
The default operator in C# returns the default value for any data type. For value types, it returns zero or false, while for reference types, it returns null. The default expressions are evaluated at compile-time, making them efficient for initialization.
Starting with C# 7.1, you can use the simplified default literal when the type can be inferred from the context.
Syntax
Following is the syntax for the traditional default operator −
default(Type)
Following is the syntax for the simplified default literal (C# 7.1+) −
Type variable = default;
Default Values for Common Types
Using default() Operator
Example
using System;
public class Demo {
public static void Main() {
int val1 = default(int);
long val2 = default(long);
bool val3 = default(bool);
double val4 = default(double);
string val5 = default(string);
char val6 = default(char);
Console.WriteLine("int default: " + val1);
Console.WriteLine("long default: " + val2);
Console.WriteLine("bool default: " + val3);
Console.WriteLine("double default: " + val4);
Console.WriteLine("string default: " + (val5 ?? "null"));
Console.WriteLine("char default: '" + val6 + "'");
}
}
The output of the above code is −
int default: 0 long default: 0 bool default: False double default: 0 string default: null char default: ''
Using default Literal (C# 7.1+)
Example
using System;
public class Program {
public static void Main() {
// Type inference with default literal
int number = default;
bool flag = default;
string text = default;
Console.WriteLine("number: " + number);
Console.WriteLine("flag: " + flag);
Console.WriteLine("text: " + (text ?? "null"));
// Using in method parameters
ProcessData(default, default);
}
static void ProcessData(int value, string message) {
Console.WriteLine($"Processing value: {value}, message: {message ?? "null"}");
}
}
The output of the above code is −
number: 0 flag: False text: null Processing value: 0, message: null
Common Use Cases
-
Variable initialization: Initialize variables to their type's default value.
-
Generic programming: Useful when working with generic types where the default value is unknown at compile time.
-
Optional parameters: Provide default values for method parameters.
-
Array initialization: Fill arrays with default values efficiently.
Example with Generic Method
using System;
public class GenericExample {
public static T GetDefaultValue<T>() {
return default(T);
}
public static void Main() {
Console.WriteLine("Default int: " + GetDefaultValue<int>());
Console.WriteLine("Default string: " + (GetDefaultValue<string>() ?? "null"));
Console.WriteLine("Default bool: " + GetDefaultValue<bool>());
}
}
The output of the above code is −
Default int: 0 Default string: null Default bool: False
Conclusion
The default operator provides a consistent way to get the default value for any type in C#. It's particularly useful in generic programming and for initializing variables. The simplified default literal introduced in C# 7.1 makes code more concise when the type can be inferred.
