值类型的数组 [英] Array of value types

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

问题描述

嗨。


我有一个struct数组。

我的问题是,如果我这样做:

STRUCT [ ] a = new STRUCT [0xA];

STRUCT s = new STRUCT();

a [0x0] = s;


由于STRUCT是一个值类型,当如上所述分配STRUCT数组的元素0

时,

是STRUCT然后复制到数组,就像通常的情况一样值类型?

我想是的。


这种结构很重。

它也是一个托管结构因此我不能使用指针。


我的选择可能是,而不是分配元素,

直接使用数组索引(很多索引然后):


STRUCT [] a =新STRUCT [0xA];

a [0x0] .PROPERTY = 0x0;

请给我一些想法。


谢谢!


-

问候,

DennisJDMyrén

奥斯陆Kodebureau

Hi.

I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value types?
I guess it is.

This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Please give me some thoughts about this.

Thank you!

--
Regards,
Dennis JD Myrén
Oslo Kodebureau

推荐答案

如果这是实际情况,你可以改变


STRUCT [] a =新STRUCT [0xA];

STRUCT s = new STRUCT();

a [0x0] = s;





STRUCT [] a =新的STRUCT [0xA];


因为数组的每个元素都是一个初始化的结构体,就像defauly构造函数已经运行一样 - 你可以在[1]中读取我的博客条目价值类型初始化的详细信息


如果不是这种情况,那么是的,你会得到一份副本,但是有任何机会发布更接近ral情况的代码,所以我们可以更具体地建议


[1] http://staff.develop.com/richardb/we...c-b73640f24314


问候


Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages。 csharp /< BC **************** @ news4.e.nsc.no>


嗨。


我有一个struct数组。

我的问题是,如果我这样做:

STRUCT [] a = new STRUCT [0xA];

STRUCT s = new STRUCT();

a [0x0] = s;


因为STRUCT是一个值类型,当分配元素时如上所述STRUCT数组的0



是STRUCT然后复制到数组,通常是值类型的情况吗?

我想是的。


这种结构很重。

它也是一个托管结构,因此我不能使用指针。


我的选择可能是,而不是分配元素,

直接使用数组索引(当时有很多索引):

STRUCT [] a =新STRUCT [0xA];

a [0x0] .PROPERTY = 0x0;

请给我对此有些想法。


谢谢!


-

问候,

Dennis JD Myr?n

奥斯陆Kodebureau


---

收到的邮件经过无病毒验证。

由AVG反病毒系统检查( http://www.grisoft.com )

版本:6.0.771 /病毒数据库:518 - 发布日期:28/09/2004


[microsoft.public。 dotnet.languages.csharp]
If this is the actual situation, you can change

STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

to

STRUCT [] a = new STRUCT [0xA];

as each element of the array will be a struct initialized as if the defauly constructor had run - you can read my blog entry at [1] for the gory details of value type initialization

If this isn''t the situation, then yes you will get a copy, but any chance of posting code that is closer to the ral situation so we can advise more concretely

[1] http://staff.develop.com/richardb/we...c-b73640f24314

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<BC****************@news4.e.nsc.no>

Hi.

I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value types?
I guess it is.

This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Please give me some thoughts about this.

Thank you!

--
Regards,
Dennis JD Myr?n
Oslo Kodebureau

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]


DennisMyrén< de **** @ oslokb.no>写道:
Dennis Myrén <de****@oslokb.no> wrote:
我有一个struct数组。
我的问题是,如果我这样做:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

因为STRUCT是一个值类型,当如上所述分配STRUCT数组的元素0时,
然后STRUCT被复制到数组,就像通常的值类型一样?
我想是的。


是的。

这种结构很重。
它也是一个托管结构,因此我不能使用指针。


它真的需要是一个结构吗?

我的选择可能是,而不是分配元素,
使用数组直接使用索引(然后很多索引):

STRUCT [] a =新STRUCT [0xA];
a [0x0] .PROPERTY = 0x0;


是的,你可以这样做 - 但如果你要设置任何重要的

数量的房产,你不会更好吗?还是有副本吗?

请给我一些想法。
I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value types?
I guess it is.
Yes.
This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.
Does it really need to be a structure then?
My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Yes, you could do that - but if you''re going to set any significant
number of properties, wouldn''t you be better off with a copy anyway?
Please give me some thoughts about this.




我建议你要做的第一件事是检查你是否*真的*需要它

是一个结构而不是一个类。我个人很少找到使用

来定义我自己的价值类型。


-

Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟

如果回复小组,请不要给我发邮件



I suggest the first thing to do is check whether you *really* need it
to be a struct rather than a class. I personally very rarely find a use
for defining my own value types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


谢谢你的回复,理查德。


我读了你的博客。好读!


-

问候,

DennisJDMyrén

Oslo Kodebureau

" Richard Blewett [DevelopMentor]" < RI ****** @ develop.com>在留言中写道

新闻:美国************** @ TK2MSFTNGP11.phx.gbl ...
Thank you for the response, Richard.

I read your blog. Good reading!

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:uS**************@TK2MSFTNGP11.phx.gbl...
如果这是实际的情况,你可以改变

STRUCT [] a =新的STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;


STRUCT [] a = new STRUCT [0xA];

因为数组的每个元素都是一个初始化的结构,就像
defauly构造函数已运行 - 您可以在[1]上阅读我的博客条目,了解值类型初始化的详细信息

如果不是这种情况,那么是的,你会得到的一个副本,但任何机会发布更接近ral情况的代码,以便我们可以更具体地建议

[1]
http://staff.develop.com/richardb /we...c-b73640f24314

问候

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

NNTP://news.microsoft.com/microsoft.public.dotnet.languages.csharp/< BC****************@news4.e.nsc.no> <无线电通信你好。我有一个结构数组。
我的问题是,如果我这样做:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

因为STRUCT是一个值类型,当分配STRUCT数组的元素0时
如上所述,
是STRUCT然后复制到数组,就像通常的值类型一样?
我想是的。

这种结构是亲切的重的。
它也是一个托管结构,因此我不能使用指针。

我的选择可能是,而不是分配元素,
直接使用数组使用索引(然后进行大量索引):

STRUCT [] a =新STRUCT [0xA];
a [0x0] .PROPERTY = 0x0;

请给我一些关于t的想法他的。

谢谢!

-
问候,
Dennis JD Myr?n
Oslo Kodebureau
由AVG反病毒系统检查( http://www.grisoft.com)
版本:6.0.771 /病毒库:518 - 发布日期:28/09/2004
[microsoft.public.dotnet.languages.csharp]
If this is the actual situation, you can change

STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

to

STRUCT [] a = new STRUCT [0xA];

as each element of the array will be a struct initialized as if the
defauly constructor had run - you can read my blog entry at [1] for the
gory details of value type initialization

If this isn''t the situation, then yes you will get a copy, but any chance
of posting code that is closer to the ral situation so we can advise more
concretely

[1]
http://staff.develop.com/richardb/we...c-b73640f24314

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<BC****************@news4.e.nsc.no>

Hi.

I have an array of struct.
My question is, if i do:
STRUCT [] a = new STRUCT [0xA];
STRUCT s = new STRUCT();
a [0x0] = s;

since STRUCT is a value type, when assigning element 0 of the STRUCT array
as above,
is the STRUCT then copied to the array, as is the usual case of value
types?
I guess it is.

This structure is kind of heavy.
It is as well a managed struct, therefore i cannot use pointers.

My option might be to, rather than assigning the elements,
to use the array directly with indexing(a lot of indexing then):

STRUCT [] a = new STRUCT [0xA];
a [0x0].PROPERTY = 0x0;
Please give me some thoughts about this.

Thank you!

--
Regards,
Dennis JD Myr?n
Oslo Kodebureau

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]



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

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