什么是默认(对象);你用C#做什么?
谷歌只是给出了这个关键词,但我偶然发现了一些代码,上面写着什么是默认(对象);你用C#做什么?,c#,C#,谷歌只是给出了这个关键词,但我偶然发现了一些代码,上面写着 MyVariable = default(MyObject); 我想知道这是什么意思。MyObject的默认值。见(MSDN): 在泛型类和方法中,出现的一个问题是如何分配 当您不知道 事先: T是引用类型还是值类型 如果T是一个值类型,那么它将是一个数值还是一个结构 给定参数化类型t的变量t,语句t=null 仅当T是引用类型且T=0仅适用于 数值类型,但不适用于结构。解决方法是使用 默认关键字,它将为引用类型返回null,为零
MyVariable = default(MyObject);
我想知道这是什么意思。MyObject的默认值。见(MSDN): 在泛型类和方法中,出现的一个问题是如何分配 当您不知道 事先:
- T是引用类型还是值类型
- 如果T是一个值类型,那么它将是一个数值还是一个结构
公共类泛型列表
{
私有类节点
{
//...
公共节点下一步;
公共数据;
}
专用节点头;
//...
公共T GetNext()
{
T温度=默认值(T);
节点电流=头;
如果(当前!=null)
{
温度=当前数据;
当前=当前。下一步;
}
返回温度;
}
}
指定的默认值
类型参数。对于
引用类型和零表示值
类型
请参见它将对象的默认值设置为变量: 引用类型为null,值类型为0。
- 对于引用类型,它返回
null
- 对于除
以外的值类型,它返回一个零初始化值可为空
- 对于
它返回空(伪空)值(实际上,这是第一个项目符号的重新声明,但值得显式表示)Nullable
default(T)
的最大用途是在泛型中,类似于Try…
模式:
bool TryGetValue(out T value) {
if(NoDataIsAvailable) {
value = default(T); // because I have to set it to *something*
return false;
}
value = GetData();
return true;
}
碰巧,我也在一些代码生成中使用它,初始化字段/变量是一件痛苦的事情——但是如果您知道类型:
bool someField = default(bool);
int someOtherField = default(int)
global::My.Namespace.SomeType another = default(global::My.Namespace.SomeType);
default
关键字对于引用类型将返回null
,对于数值类型将返回zero
对于struct
s,它将返回初始化为零或null的结构的每个成员,具体取决于它们是值类型还是引用类型
也许这可以帮助您:
using System;
using System.Collections.Generic;
namespace Wrox.ProCSharp.Generics
{
public class DocumentManager < T >
{
private readonly Queue < T > documentQueue = new Queue < T > ();
public void AddDocument(T doc)
{
lock (this)
{
documentQueue.Enqueue(doc);
}
}
public bool IsDocumentAvailable
{
get { return documentQueue.Count > 0; }
}
}
}
默认关键字有多种含义,具体取决于使用它的上下文。开关
语句使用默认值来定义默认情况,对于泛型,默认值用于将泛型类型初始化为null或0,具体取决于它是引用类型还是值类型。default关键字为请求类型的变量返回“default”或“empty”值 对于所有引用类型(用
类
、委托
等定义),这是null
。对于值类型(用struct
,enum
等定义),它是一个全零值(例如,int 0
,DateTime 0001-01-01 00:00
等)
它主要与可应用于引用类型和值类型的泛型代码一起使用,因为不能将
null
赋值给值类型变量。如果未应用约束将泛型类型参数限制为引用类型,则还可以传递值类型,例如结构。在这种情况下,将类型参数与null进行比较将始终为false,因为结构可以为空,但不能为null
错误代码
public void TestChanges<T>(T inputValue)
try
{
if (inputValue==null)
return;
//operation on inputValue
}
catch
{
// ignore this.
}
}
public void TestChanges(T inputValue)
尝试
{
如果(inputValue==null)
返回;
//输入值操作
}
抓住
{
//忽略这一点。
}
}
纠正
public void TestChanges<T>(T inputValue)
try
{
if (object.Equals(inputValue, default(T)) )
return;
//operation on inputValue
}
catch
{
// ignore this.
}
}
public void TestChanges(T inputValue)
尝试
{
if(object.Equals(inputValue,默认值(T)))
返回;
//输入值操作
}
抓住
{
//忽略这一点。
}
}
默认值(T)
的另一个很好的用法是当编译器无法确定返回类型时,如这里所示
X类
{
公共int?P{get;set;}
}
//以代码分配
var x=新的x();
//考虑COL[ [键] ]返回对象装箱值
//数据读取器就是这样一种情况
x、 P=myReader[“someColumn”]==DbNull.Value?默认值(int?)(int)myReader[“someColumn”];
如果我使用属性int n
创建类Foo
。我可以“重载”default
,使其设置为n
说5
,而不是0
?@机器魔术师:不。你不能重载default
。不是int foo=default(int)代码>相同的是intfoo代码>?Ie don not uninitialized ints default与default(int)
?@AdamParkin具有相同的值,这取决于您谈论的是字段还是局部变量;是,字段初始化为零d空间,与default(…)
相同;局部变量没有默认值(虽然从技术上讲,IL中的
.locals init
意味着它们将再次默认为零,但您需要使用不安全的机制来观察它)“它返回空(psuedo null)值,该值…那是什么?看起来句子没有结束。
using System;
using System.Collections.Generic;
namespace Wrox.ProCSharp.Generics
{
public class DocumentManager < T >
{
private readonly Queue < T > documentQueue = new Queue < T > ();
public void AddDocument(T doc)
{
lock (this)
{
documentQueue.Enqueue(doc);
}
}
public bool IsDocumentAvailable
{
get { return documentQueue.Count > 0; }
}
}
}
public T GetDocument()
{
T doc = default(T);
lock (this)
{
doc = documentQueue.Dequeue();
}
return doc;
}
public void TestChanges<T>(T inputValue)
try
{
if (inputValue==null)
return;
//operation on inputValue
}
catch
{
// ignore this.
}
}
public void TestChanges<T>(T inputValue)
try
{
if (object.Equals(inputValue, default(T)) )
return;
//operation on inputValue
}
catch
{
// ignore this.
}
}