473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What happens to a reference type in a valuetype struct?

I was just curious. I know that a struct is value type by definition,
and will be placed on the stack, but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?

Oct 23 '06 #1
3 9482
I was just curious. I know that a struct is value type by definition,
and will be placed on the stack, but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?
The reference type is actually allocated on the heap but the reference is
on the stack (in the value type) -- assuming that your value type is on the
stack.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 23 '06 #2

DaTurk wrote:
I was just curious. I know that a struct is value type by definition,
and will be placed on the stack, but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?
The value type will be big enough to accommodate a reference (pointer)
to the reference type (plus anything else in the value type). The
storage for the state (fields) of the reference type itself will always
be on the heap.

By the way, this type of construct is possible, but it's usually best
that the reference type in question be immutable. If the state (fields,
contents) of the reference type can be changed then the value type will
act oddly, to say the least.

I do just what you described to implement a Measure: a quantity coupled
with a unit of measure (for example 5 combined with UnitOfMeasure.Feet
to represent five feet). The Measure is a struct, but the UnitOfMeasure
is a class. The important point is that the UnitOfMeasure class has no
properties that can be set, and its methods never change it: once it's
constructed it's immutable. This keeps the Measure struct acting in
reasonable ways.

Oct 23 '06 #3
DaTurk <mm******@hotmail.comwrote:
I was just curious. I know that a struct is value type by definition,
and will be placed on the stack
No, not necessarily. A value type is stored in the same context as it
is declared: if it's a member of a reference type, for instance, it
will be on the heap, as part of the data for the object.
but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?
The reference is placed in the same context as the rest of the data for
the struct. The object that the reference refers to will certainly be
on the heap.

See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher | last post by:
Hi all, I am trying to store a reference to a variable in my data providerr class but cannot save a reference to it. Can anyone help me? I would like this code to allow me to store any field...
4
by: pervinder | last post by:
Hi, I get an error on line #3 while compiling the program segment on aix (xlc - va6) While this warning is not seen with gcc "test.cpp" line 200.18: 1540-0152 (W) A template dependent name that...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
59
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
70
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static...
1
by: Anonymous | last post by:
I have been trying to write a template class for a shared memory container, over the last few days - but I keep getting numerous compiler errors e.g: Error: syntax error : identifier...
21
by: Ray Cassick | last post by:
I can't believe why I had not noticed this before and why I never asked it before... When defining a string why is it not required to use the new keyword? Like: Dim a As String = New String ...
6
by: kbin | last post by:
Hi all, I'm wondering if it is possible to have some sort of soft reference to an object's properties by using a string representing the property name. Let me explain by an example, here is my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.