包含数组的struct? [英] Struct that contains an array?

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

问题描述

您好!


我有以下课程:


public struct Move

{

public int来自{get;组; }


public int To {get;组; }


public int Captures {get;组; }

}


公共类MoveList

{

private Move [] moves = new Move [32];


public int Length {get;私人集; }


public void添加(移动移动)

{

移动[长度++] =移动;

}


public移动这个[int index]

{

get

{

返回移动[index];

}

}

}


MoveList似乎是一个结构的候选者。但是,我似乎无法创建一个包含数组的结构。这可能吗?如果是这样,

如何做到这一点?


提前致谢!


-

Daniel

解决方案

创建带数组的结构没什么特别的 -

应该正常工作按原样 - 但是,我不同意MoveList会使
成为一个好的结构 - 添加()等建议可变,这是

*总是*结构中的一个坏主意;特别是,使用

结构的期望是它具有值类型语义,但是存在一个

可变数组会将两个孤立的结构绑定在一起意外

方式。


实际上,(正确地)在.NET中创建一个结构

是非常/罕见的。只是把它作为一个类...你应该只为(不可变的)值单位创建结构

,例如货币

值。 (不可变地组合小数和货币代码),或时间

范围 (不可分割地结合2个DateTimes,或者一个DateTime和

TimeSpan)。


Marc


8月19日早上8点50分,Daniel Lidstr?m< some ... @ microsoft.comwrote:


我有以下类:


* * public struct Move

* * {

* * * public int From {get;组; }


* * * public int To {get;组; }


* * * public int Captures {get;组; }

* *}



可变结构确实是坏消息。我建议你把它变成一个

类,或者让它变成不可变的。


* * public class MoveList

* * {

* * * private Move [] moves = new Move [32];


* * * public int Length {get;私人集; }


* * * public void添加(移动移动)

* * * {

* * * * *移动[长度++] =移动;

* * *}

* * * public移动此[int index]

* * * {

* * * * *获得

* * * * * {

* * * * * *返回移动[index];

* * * * *}

* * *}

* *}


MoveList似乎成为一个结构的候选人。



不是我 - 再次,它是可变的。为什么你特别想要它?
是一个结构?


但是,我看起来不可能

创建包含数组的结构。



我不明白为什么不。你不能使用实例初始化器。


这可能吗?如果是这样,那怎么做呢?



嗯,你需要意识到没有办法阻止移动

为空 - 结构总是有一个默认的构造函数,

只是将所有字段设置为默认值(在这种情况下为null)。

你可以使数组延迟分配(在获取时或添加)

但我真的不相信这应该是第一个

地方的结构。


Jon


哦,顺便说一句:Move也应该是一个类,或者应该是

不可变的;例如,以下Move可以作为结构

(虽然我仍然会质疑为什么它不仅仅是一个类):


public struct Move

{

private readonly int from,to,captues;

public int From {get {return from;}}

public int To {get {return to;}}

public int Captures {get {return captues;}}

public Move(int from ,int to,int capture)

{

this.from = from;

this.to = to;

this.captures = captures;

}

}


(再次,我仍然认为这看起来更像一个班级比一个结构 - 它

isn''ta" measure",这是最常见的结构场景。)


Marc

Hello!

I have these following classes:

public struct Move
{
public int From { get; set; }

public int To { get; set; }

public int Captures { get; set; }
}

public class MoveList
{
private Move[] moves = new Move[32];

public int Length { get; private set; }

public void Add(Move move)
{
moves[Length++] = move;
}

public Move this[int index]
{
get
{
return moves[index];
}
}
}

MoveList seems to be a candidate for being a struct. However, I can''t seem
to create a struct that contains an array. Is this possible? If so, how does
one do it?

Thanks in advance!

--
Daniel

解决方案

There is nothing special about creating a struct with an array - it
should work fine "as is" - however, I disagree that MoveList would
make a good struct - the Add() etc suggest mutablitly, which is
*always* a bad idea in a struct; in particular, the expectation with a
struct is that it has value-type semantics, but the presence of a
mutable array would bind two isolated structs together in unexpected
ways.

Actually, it is /incredibly/ rare to (correctly) create a struct
in .NET; just leave it as a class... you should only create structs
for (immutable) "value units", for example a "currency
value" (immutably combining a decimal and a currency code), or a "time
range" (immutably combining either 2 DateTimes, or a DateTime and
TimeSpan).

Marc


On Aug 19, 8:50*am, Daniel Lidstr?m <some...@microsoft.comwrote:

I have these following classes:

* *public struct Move
* *{
* * * public int From { get; set; }

* * * public int To { get; set; }

* * * public int Captures { get; set; }
* *}

Mutable structs are really bad news. I suggest you either make this a
class, or make it immutable.

* *public class MoveList
* *{
* * * private Move[] moves = new Move[32];

* * * public int Length { get; private set; }

* * * public void Add(Move move)
* * * {
* * * * *moves[Length++] = move;
* * * }

* * * public Move this[int index]
* * * {
* * * * *get
* * * * *{
* * * * * * return moves[index];
* * * * *}
* * * }
* *}

MoveList seems to be a candidate for being a struct.

Not to me - again, it''s mutable. Why would you particularly want it to
be a struct?

However, I can''t seem
to create a struct that contains an array.

I don''t see why not. You just can''t use an instance initializer.

Is this possible? If so, how does one do it?

Well, you''d need to realise that there''s no way of stopping "moves"
from being null - structs always have a default constructor, which
just sets all fields to default values (i.e. null in this case).
You could make the array lazily allocated (when fetching or adding)
but really I''m not convinced this should be a struct in the first
place.

Jon


Oh, by the way: Move should also be a class, or should be made
immutable; for example, the following Move would be OK as a struct
(although I''d still question why it isn''t simply a class):

public struct Move
{
private readonly int from, to, captues;
public int From { get {return from;}}
public int To { get {return to;}}
public int Captures { get {return captues;}}
public Move(int from, int to, int captures)
{
this.from = from;
this.to = to;
this.captures = captures;
}
}

(again, I still think this looks more like a class than a struct - it
isn''t a "measure", which is the most common struct scenario).

Marc


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

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