472,784 Members | 1,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 9186
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.