<p>default关键字提供类型安全的默认值,对值类型返回零值(如0、false),对引用类型返回null;在泛型中统一处理不同类型初始化,避免使用null带来的类型不安全问题;C# 7.1+支持default字面量实现简洁赋值,C# 8.0+可在switch表达式中作为默认分支返回对应类型的默认状态。</p>

C#中的
default
int
bool
string
T
要指定默认值,最直接的方式就是使用
default
对于值类型(
int
double
bool
struct
default
default(int)
0
default(bool)
false
对于引用类型(
string
object
default
null
在C# 7.1及更高版本中,你还可以使用更简洁的
default
default literal
示例:
int myInt = default(int); // myInt 会是 0 string myString = default(string); // myString 会是 null bool myBool = default(bool); // myBool 会是 false // C# 7.1+ 的简化写法: int anotherInt = default; // 同样是 0 MyClass myObject = default; // 同样是 null (假设 MyClass 是一个类)
这种方式特别强调了类型安全和代码的普适性,尤其是在你无法预知具体类型,或者想表达“给我这个类型最原始、最未经初始化的状态”时。
default
泛型编程,说白了就是写一套代码能适配多种数据类型,但这里面有个让人头疼的问题:你不知道
T
int
string
T
0
null
想象一下,如果你要创建一个泛型列表,需要在内部初始化数组元素。如果
T
int
0
T
string
null
if (T is int) ... else if (T is string) ...
default(T)
T
T
default(T)
举个例子,我以前写一个泛型缓存类,需要预分配一个
T
public class SimpleCache<T>
{
private T[] _items;
private int _capacity;
public SimpleCache(int capacity)
{
_capacity = capacity;
_items = new T[capacity];
// 关键来了,如何初始化这些槽位?
// 如果没有default(T),我真的不知道该怎么办
for (int i = 0; i < capacity; i++)
{
_items[i] = default(T); // 这行代码简直是救星
}
}
// ... 其他方法
}这行
_items[i] = default(T);
T
int
_items[i]
0
T
MyObject
null
default
default
null
default
null
null
int?
null
null
int i = null;
而
default
default(MyClass)
null
string s = default;
string s = null;
default(int)
0
default(bool)
false
default
null
上下文应用差异:
null
MyObject obj = null; // 明确表示obj当前没有实例
default
// 在泛型方法中返回一个T的默认值
public T GetDefault<T>()
{
return default; // T可能是int,也可能是MyClass
}default
int count = default; // 等同于 count = 0; List<string> names = default; // 等同于 names = null;
简而言之,
null
default
null
default
default
一个很常见的场景是在可选参数中。从C# 7.1开始,我们可以用
default
public void ProcessItem<T>(T item = default)
{
// 如果调用时没有提供item,它就会是T的默认值
if (item is null) // 对于引用类型,可以这样判断
{
Console.WriteLine("Item is null or default for its type.");
}
else if (EqualityComparer<T>.Default.Equals(item, default(T))) // 对于值类型,这样判断
{
Console.WriteLine("Item is default for its type (e.g., 0 for int).");
}
else
{
Console.WriteLine($"Processing item: {item}");
}
}
// 调用示例:
ProcessItem<int>(); // item 是 0
ProcessItem<string>(); // item 是 null
ProcessItem<int>(10); // item 是 10
ProcessItem<string>("hello"); // item 是 "hello"这让泛型方法的默认参数处理变得非常灵活,避免了为值类型和引用类型编写重载。
另一个非常有用的场景是在C# 8.0引入的switch
default
public string GetStatusCodeDescription(int code) => code switch
{
200 => "OK",
404 => "Not Found",
_ => default // 这里的default是string的默认值,也就是null
};
// 调用:
Console.WriteLine(GetStatusCodeDescription(200)); // 输出 "OK"
Console.WriteLine(GetStatusCodeDescription(500)); // 输出 "" (因为string的default是null,Console.WriteLine会打印空字符串)在这个
switch
_
default
string
null
_ => null
int?
public int? ParseNullableInt(string s) => s switch
{
"one" => 1,
"two" => 2,
_ => default // 这里的default是int?的默认值,也就是null
};这种用法非常优雅,它让代码更具表达力,并且减少了冗余。
default
以上就是C#的default关键字有什么用途?如何指定默认值?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号