> Value types (structs) are intended to be used where a lightweight (few > members, little instance data, generally do not persist for long) type is > required. It's also not well designed for huge collections of data. > Value types are allocated from the stack rather than from the heap. So, > for large collections of large value types you need to consider the stack > ramifications.
Value types are only allocated on the stack in some cases (http://www.pobox.com/~skeet/csharp/memory.html). "Collections of value types" are indeed typically not allocated on the stack, so you don't need to consider the stack ramifications there. (Unless you are talking about collections in the sense of a large number of local variable declarations, but I don't think you will easily run into stack problems with local variable declarations.) In particular, in code similar to the following, the array will not be allocated on the stack, even if array is a local variable: int[] array = new int[10000]; The same applies with newly defined value types: struct S { } S[] array = new S[10000]; IIRC, this can be much more memory efficient than the equivalent: class C { } C[] array = new C[10000]; for (int i = 0; i < 10000; ++i) { array[i] = new C(); } The struct way is more runtime efficient, too, because value types can be initialized without their constructors being run [1]. Fabian [1] The public default constructor of value types can be simulated by filling a block of memory with zeroes. =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com