如何覆盖默认(T)在C#中? [英] How to override default(T) in C#?

查看:116
本文介绍了如何覆盖默认(T)在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/5088682/howto-change-what-defaultt-returns-in-c-sharp">Howto改一下默认(T)返回在C#

 打印(默认值(INT)== 0)//真
 

同样地,如果我有一个自定义的对象,其默认值为null。

 打印(默认值(美孚)== NULL)//真
 

我可以有默认(美孚)和not null?

自定义值

例如,这样的事情:

 公共静态覆盖美孚默认设置()
{
    返回新的Foo();
}
 

这不会编译。谢谢..

解决方案

您不能覆盖​​默认(T)的关键字。它始终是引用类型和值类型零空。

更多信息

Possible Duplicate:
Howto change what Default(T) returns in C#

print(default(int) == 0) //true

Similarly if I have a custom object, its default value will be null.

print(default(Foo) == null) //true

Can I have a custom value for default(Foo) and not null?

For example, something like this:

public static override Foo default()
{
    return new Foo();
}

This wont compile. Thanks..

解决方案

You can't override the default(T) keyword. It is always null for reference types and zero for value types.

More Information

这篇关于如何覆盖默认(T)在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆