C#2005中的可空值类型 [英] nullable value types in C# 2005

查看:60
本文介绍了C#2005中的可空值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚看了一篇关于使用可空值类型的文章。 (值

类型可以有效地没有价值而且没有设置)。


语法是在值中添加一个问号标记

声明,例如:


int?年龄;


我不喜欢那么多,我认为使用

新关键字会更加一致,例如可空 ;。但无论如何...


对于实际安装了测试版的人来说,有几个问题:


1.什么是开销声明您的值类型可以为空?

2.它们是否与非可空值类型兼容? (即我可以将

分配给另一个吗?)。


谢谢,

John

I was just looking at an article about using nullable value types. (value
types that can effectively have no value and not be set).

The syntax is to append a question-mark to the value type in the
declaration, eg:

int? age;

I don''t like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...

A couple of questions for anyone who actually has the beta installed:

1. What''s the overhead of declaring your value types nullable?
2. Are they type-compatible with non-nullable value types? (ie. can I assign
one to the other?).

Thanks,
John

推荐答案

> 1.声明值类型可以为空的开销是多少?


它们是作为库结构Nullable的实例实现的,所以int?

实际上只是Nullable< int>。在里面,有一个bool记录

定义/未定义状态和int来存储数据。
> 1. What''s the overhead of declaring your value types nullable?

They are implemented as an instance of the library struct Nullable, so int?
is really just Nullable<int>. Inside, there is a bool to record
defined/undefined state and the int to store the data.
2.它们是否与非类型兼容可空值类型? (即我可以将
分配给另一个吗?)。
2. Are they type-compatible with non-nullable value types? (ie. can I assign
one to the other?).




从int到int?有一个隐含的转换。


在另一个方向是明确的(即你需要强制转换),因为它可能会导致
失败(即int?can)是null这是int无法处理的值。


类型转换故事还有很多可以为nullable的类型,但是

''除非你有一些特定的问题,否则你可能已经足够了。


Mark



From int to int? there is an implicit conversion.

In the other direction is it explicit (i.e. you need to cast) since it might
fail (i.e. the int? could be null which is a value the int cannot handle).

There is a lot more to the type conversion story with nullable types, but
that''s probably enough for the moment unless you have some specific question
in mind.

Mark


当你使用说


int? i = null;


你真正说的是


Nullable< int> i = null;


现在为Nullable< T>是一种通用的值类型。它包含值(在本例中为int)和一个布尔标志,用于说明值是否已设置。所以在开销方面你只需要在结构中添加一个布尔标志。


至于与非可空类型的兼容性,你可以写一下


int? i = 5;

if(i.HasValue)

{

int j =(int)i;

int k = i.Value;

}


这两种结构都有效。但是,如果您不首先执行检查并且只是尝试强制转换或赋值 - 并且可空类型为null - 那么您将获得InvalidOperationException


问候


Richard Blewett - DevelopMentor
http:// www.dotnetconsult.co.uk/weblog
http:// www .dotnetconsult.co.uk


我刚看了一篇关于使用可空值类型的文章。 (值

类型可以有效地没有价值而且没有设置)。


语法是在值中添加一个问号标记

声明,例如:


int?年龄;


我不喜欢那么多,我认为使用

新关键字会更加一致,例如可空 ;。但无论如何...


对于实际安装了测试版的人来说,有几个问题:


1.什么是开销声明您的值类型可以为空?

2.它们是否与非可空值类型兼容? (即我可以将

分配给另一个吗?)。


谢谢,

John

When you use say

int? i = null;

what you are really saying is

Nullable<int> i = null;

Now Nullable<T> is a value type generic. It contains the value (in this case an int) and a boolean flag to state whether the value has been set or not. So in terms of overhead you are simply adding a boolean flag to the structure.

As far as compatibility to teh non-nullable types, you can write

int? i = 5;
if( i.HasValue )
{
int j = (int)i;
int k = i.Value;
}

both of these constructs work. However, if you do not perform the check first and simply try to cast or assign the Value - and the nullable type is null - then you will get an InvalidOperationException

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I was just looking at an article about using nullable value types. (value
types that can effectively have no value and not be set).

The syntax is to append a question-mark to the value type in the
declaration, eg:

int? age;

I don''t like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...

A couple of questions for anyone who actually has the beta installed:

1. What''s the overhead of declaring your value types nullable?
2. Are they type-compatible with non-nullable value types? (ie. can I assign
one to the other?).

Thanks,
John


出于某种原因我是否想再将它分配为null,那么

就是这种情况。有没有办法将标志重置为false?或者一旦它设定为b $ b,它是永久性的吗?我总是可以自己测试但是我不在我的电脑上

家里所以我问。

谢谢,

Shawn

" Richard Blewett [DevelopMentor]" < RI ****** @ NOSPAMdevelop.com>写在

消息新闻:O2 ************** @ TK2MSFTNGP15.phx.gbl ...
What about whether I want to assign it to null again for some reason, there
are such cases. Is there a way to reset the flag to false? Or once it is
set, it is permanent? I can always test it myself but I am not at my PC at
home so I ask.
Thanks,
Shawn
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:O2**************@TK2MSFTNGP15.phx.gbl...
当你用say

int? i = null;

你真正说的是

Nullable< int> i = null;

现在为Nullable< T>是一种通用的值类型。它包含值(在此
情况下为int)和一个布尔标志,用于说明值是否已设置或

不是。所以在开销方面你只需要在

结构中添加一个布尔标志。
至于与非可空类型的兼容性,你可以写一下

诠释? i = 5;
if(i.HasValue)
{j /(int)i;
int k = i.Value;
}
这两种结构都有效。但是,如果你不首先执行检查
并且只是尝试强制转换或赋值 - 并且可空类型是

null - 那么你将得到一个InvalidOperationException
问候

Richard Blewett - DevelopMentor
http:// www.dotnetconsult.co.uk/weblog
http:// www .dotnetconsult.co.uk

我刚看了一篇关于使用可空值类型的文章。
(可以有效地没有值且不能设置的值类型)。

语法是在
声明中的值类型附加一个问号,例如:

int?年龄;

我不喜欢那么多,我认为使用
新关键字会更加一致,例如可空。但无论如何......

对于实际安装了测试版的人来说,有几个问题:

1.声明值类型可以为空的开销是多少? br /> 2.它们是否与非可空值类型兼容? (即我可以将b $ b分配给另一个吗?)。

谢谢,
John
When you use say

int? i = null;

what you are really saying is

Nullable<int> i = null;

Now Nullable<T> is a value type generic. It contains the value (in this case an int) and a boolean flag to state whether the value has been set or
not. So in terms of overhead you are simply adding a boolean flag to the
structure.
As far as compatibility to teh non-nullable types, you can write

int? i = 5;
if( i.HasValue )
{
int j = (int)i;
int k = i.Value;
}

both of these constructs work. However, if you do not perform the check first and simply try to cast or assign the Value - and the nullable type is
null - then you will get an InvalidOperationException
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set).

The syntax is to append a question-mark to the value type in the
declaration, eg:

int? age;

I don''t like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...

A couple of questions for anyone who actually has the beta installed:

1. What''s the overhead of declaring your value types nullable?
2. Are they type-compatible with non-nullable value types? (ie. can I assign one to the other?).

Thanks,
John



这篇关于C#2005中的可空值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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