默认值 [英] default values

查看:95
本文介绍了默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管CSC尽力检测未使用的未分配变量,但它确实会错过它...例如,如果你只是在一个类中声明一个双重

没有分配默认值,它的默认值为0,让你无论如何都要使用它。


我的问题是,如何检索默认值给定类型的值?

CLR显然将这些默认值存储在某个地方,我希望能够获得

持有它。


我希望以下代码可行,但TypeInitializer为null:


double d = 12;

ConstructorInfo con = d.GetType()。TypeInitializer;

Console.WriteLine(con.Invoke(new object [] {}));


有什么想法吗?

Even though CSC does its best to detect use of unassigned variables, it
often misses it... for example if you just declare a double in a class
without assigning a default value, it has a default value of 0 and lets you
use it anyway.

My question is, how can I retrieve the default value for a given type? The
CLR obviously has these defaults stored somewhere and I was hoping to get
hold of it.

I hoped the following code would work, but TypeInitializer is null:

double d = 12;
ConstructorInfo con = d.GetType().TypeInitializer;
Console.WriteLine(con.Invoke(new object[] { }));

Any ideas?

推荐答案

John,
John,
我的问题是,如何检索给定类型的默认值?
My question is, how can I retrieve the default value for a given type?




默认是所有位清零,对于数字

类型为0或0.0,对于bools为false,对于引用类型为null。


对于值类型,当你使用默认的

构造函数时,你会得到默认值。


double d = new double(); //实际上与双d = 0.0相同;


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net / dotnet / | http://www.dotnetinterop.com

请回复到新闻组。



The default is all bits zeroed out, meaning 0 or 0.0 for numeric
types, false for bools and null for reference types.

For value types, you get the default value when using the default
constructor.

double d = new double(); // effectively the same as double d = 0.0;

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


是的,这可能是真的 - 但是,给定一个任意值类型,我如何以编程方式检索默认值
该类型的价值?它不能只是
0位,因为对于一个不起作用的字符串(例如)。


我会有希望值类型的盒装等价物将具有

构造函数,但GetConstructor返回null。


" Mattias Sj?gren" <毫安******************** @ mvps.org>在留言中写道

news:e4 ************** @ TK2MSFTNGP11.phx.gbl ...
Yes, that may be true -- but, given an arbitrary value type, how do I
programmatically retrieve the default value for that type? It can''t just be
0 bits, because for a string (for example) that won''t work.

I''d have hoped that the boxed equivalent of the value type would have a
constructor, but GetConstructor returns null.

"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
John,
我的问题是,如何检索给定类型的默认值?
My question is, how can I retrieve the default value for a given type?



默认为所有位清零,表示0或0.0表示数字类型,bools为false,引用类型为null。

对于值类型,使用默认的
构造函数时会得到默认值。
double d = new double(); //实际上与双d = 0.0相同;

Mattias

-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请仅回复新闻组。



The default is all bits zeroed out, meaning 0 or 0.0 for numeric
types, false for bools and null for reference types.

For value types, you get the default value when using the default
constructor.

double d = new double(); // effectively the same as double d = 0.0;

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



John,


实际上非常简单。您可以使用反射来确定

是否以某种方式从ValueType派生类型。如果确实如此,那么你就可以使用默认构造函数来生成一个值为零/
的值。如果它不是从ValueType派生的,那么它是一个参考

类型,默认值为null。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" John Wood" < SP ** @ isannoying.com>在消息中写道

新闻:uu ************** @ TK2MSFTNGP09.phx.gbl ...
John,

It''s actually quite simple. You can use reflection to determine whether
or not the type derives from ValueType in some way. If it does, then you
can just use the default constructor to generate a value with the bits
zeroed out. If it does not derive from ValueType, then it is a reference
type, and the default value is null.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Wood" <sp**@isannoying.com> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
是的,可能是true - 但是,给定一个任意值类型,我如何以编程方式检索该类型的默认值?它不能只是
是0位,因为对于一个不起作用的字符串(例如)。

我希望盒装等效的值type将有一个
构造函数,但GetConstructor返回null。

Mattias Sj?gren <毫安******************** @ mvps.org>在消息中写道
新闻:e4 ************** @ TK2MSFTNGP11.phx.gbl ...
Yes, that may be true -- but, given an arbitrary value type, how do I
programmatically retrieve the default value for that type? It can''t just be 0 bits, because for a string (for example) that won''t work.

I''d have hoped that the boxed equivalent of the value type would have a
constructor, but GetConstructor returns null.

"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:e4**************@TK2MSFTNGP11.phx.gbl...
John,
John,
我的问题是,如何检索给定类型的默认值?
My question is, how can I retrieve the default value for a given type?



默认值是所有位清零,表示0或0.0表示数字
类型,bools为false,引用类型为null。

对于值类型,使用默认的
构造函数时会得到默认值。

double d = new double(); //实际上与双d = 0.0相同;

Mattias

-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请仅回复新闻组。



The default is all bits zeroed out, meaning 0 or 0.0 for numeric
types, false for bools and null for reference types.

For value types, you get the default value when using the default
constructor.

double d = new double(); // effectively the same as double d = 0.0;

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.




这篇关于默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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