结构中的数组 [英] Arrays in structures
问题描述
我知道结构是值类型,数组和类是
引用类型。但是作为结构成员的数组呢,比如
C
struct x
{
int n;
int a [100];
}
这样的结构如何(例如在C ++中声明为一个班级中的一个字段)
用C#访问?
Peter Seaman
<" Peter Seaman" < StableSoftware.com上的Peter MS Seaman>>写道:我理解结构是值类型,数组和类是引用类型。但是作为结构成员的数组怎么样呢?
结构x
{/ n>
int a [100];
你不能在C#中声明它 - 你必须声明它只是
int [] a;
并在构造函数或类似函数中初始化它。结构本身仍然是一个值类型,但变量a将包含一个引用。它与
完全相同,如果你有一个包含字符串引用的结构
或类似的结果。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件
>并在构造函数或类似函数中初始化它。结构本身是仍然是值类型,但变量a将包含引用。它与
或类似的结构完全一样。
值类型仅存在于堆栈中,但是包含String的struct
变量是什么?
当我们离开函数时,这个String会发生什么?
Gawel
Gawelek< ga ***** @ NOSPAMEKpoczta.gazeta.pl>写道:并在构造函数或类似函数中初始化它。结构本身仍然是值类型,但变量a将包含引用。它的结构与包含字符串引用或类似结构的结构完全一样。
值类型只存在于堆栈上
不,他们不是。请参阅
http://www.pobox.com/ ~sibet / csharp / memory.html
但是包含String变量的struct会怎么样?
当我们离开函数时,这个String会发生什么?
它与以往一样:如果没有对字符串的实时引用,
它最终将被垃圾收集。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件
I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C
struct x
{
int n;
int a[100];
}
How is such a structure (e.g. declared in C++ as a field in a class)
accessed in C#?
Peter Seaman解决方案<"Peter Seaman" <Peter MS Seaman at StableSoftware.com>> wrote:I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C
struct x
{
int n;
int a[100];
}
How is such a structure (e.g. declared in C++ as a field in a class)
accessed in C#?
You can''t declare it like that in C# - you''d have to declare it as just
int[] a;
and initialise it in a constructor or similar. The struct itself is
still a value type, but the variable a would contain a reference. It''s
exactly the same as if you have a struct containing a string reference
or similar.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
> and initialise it in a constructor or similar. The struct itself isstill a value type, but the variable a would contain a reference. It''s
exactly the same as if you have a struct containing a string reference
or similar.
Value types live only on stack, but what with struct containing String
variable?
What will happen with this String when for example we leave function ?
Gawel
Gawelek <ga*****@NOSPAMEKpoczta.gazeta.pl> wrote:and initialise it in a constructor or similar. The struct itself is
still a value type, but the variable a would contain a reference. It''s
exactly the same as if you have a struct containing a string reference
or similar.
Value types live only on stack
No they don''t. See
http://www.pobox.com/~skeet/csharp/memory.html
but what with struct containing String variable?
What will happen with this String when for example we leave function ?
It''s the same as always: if there are no live references to the string,
it will eventually be garbage collected.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于结构中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!