C#中`default`关键字的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1727346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of `default` keyword in C#?
提问by user196546
- What is the use of
default
keyword in C#? - Is it introduced in C# 3.0 ?
default
C#中关键字的作用是什么?- 它是在 C# 3.0 中引入的吗?
采纳答案by Andrew Hare
The default
keyword is contextual since it has multiple usages. I am guessing that you are referring to its newer C# 2 meaning in which it returns a type's default value. For reference types this is null
and for value types this a new instance all zero'd out.
该default
关键字是上下文相关的,因为它有多种用法。我猜你指的是它较新的 C# 2 含义,它返回类型的默认值。对于引用类型这是null
一个新实例,对于值类型这是一个全部归零的新实例。
Here are some examples to demonstrate what I mean:
以下是一些示例来说明我的意思:
using System;
class Example
{
static void Main()
{
Console.WriteLine(default(Int32)); // Prints "0"
Console.WriteLine(default(Boolean)); // Prints "False"
Console.WriteLine(default(String)); // Prints nothing (because it is null)
}
}
回答by jrista
The defaultkeyword has different semantics depending on its usage context.
该默认关键字取决于它的使用环境不同的语义。
The first usage is in the context of a switch statement, available since C# 1.0:
http://msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx
第一种用法是在 switch 语句的上下文中,从 C# 1.0 开始可用:http:
//msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx
The second usage is in the context of generics, when initializing a generic type instance, available since C# 2.0:
http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx
第二种用法是在泛型的上下文中,在初始化泛型类型实例时,自 C# 2.0 起可用:http:
//msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx
回答by Mehdi Golchin
You can use default to obtain the default value of a Generic Type
as well.
您也可以使用 default 来获取 a 的默认值Generic Type
。
public T Foo<T>()
{
.
.
.
return default(T);
}
回答by Marc Gravell
The most common use is with generics; while it worksfor "regular" types (i.e. default(string)
etc), this is quite uncommon in hand-written code.
最常见的用途是泛型;虽然它适用于“常规”类型(即default(string)
等),但这在手写代码中并不常见。
I do, however, use this approach when doing code-generation, as it means I don't need to hard-code all the different defaults - I can just figure out the type and use default(TypeName)
in the generated code.
但是,我确实在进行代码生成时使用这种方法,因为这意味着我不需要对所有不同的默认值进行硬编码——我可以default(TypeName)
在生成的代码中找出类型和使用。
In generics, the classic usage is the TryGetValue
pattern:
在泛型中,经典用法是TryGetValue
模式:
public static bool TryGetValue(string key, out T value) {
if(canFindIt) {
value = ...;
return true;
}
value = default(T);
return false;
}
Here we haveto assign a value to exit the method, but the caller shouldn't really care what it is. You can contrast this to the constructor constraint:
这里我们必须分配一个值来退出方法,但调用者不应该真正关心它是什么。您可以将其与构造函数约束进行对比:
public static T CreateAndInit<T>() where T : ISomeInterface, new() {
T t = new T();
t.SomeMethodOnInterface();
return t;
}
回答by prasanna venkatesh
"default" keyword (apart from switch-case) helps you initialize the instance of an object like class,list and more types It is used because of its generic property where it helps you to assign the types default value when you do not know its value as advance way to avoid mistakes in your further(future) code.
“default”关键字(除了switch-case)可以帮助你初始化一个对象的实例,比如类,列表和更多类型它的使用是因为它的通用属性,当你不知道它的时候,它可以帮助你分配类型默认值价值作为避免进一步(未来)代码中错误的高级方法。
回答by Rodney
Echoing and emphasizing it's use in generics and little else other than code-generation.
呼应并强调它在泛型中的使用,除了代码生成之外几乎没有其他用途。
If you have to initialize to default (already suspiciously smelly in my book) be clear. Just do it.
如果你必须初始化为默认值(在我的书中已经很可疑了)要清楚。去做就对了。