473,387 Members | 1,464 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,387 software developers and data experts.

Structs and Ref types

Hey All,

This may be a simple one but I thought I would throw it out there
because I can't find an answer. Structures are value types as we know.
Thus they are stored on the stack. What happenes though, if you have a
struct that contains a ref type such as a string? Is the struct still
a value type? Or is it now stored in heap space?

Brette.Net

Aug 1 '07 #1
3 1467
The struct instance is stored in the stack. The reference type instance is
stored in the heap. The struct only contains a reference to the actual
instance.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<Br********@gmail.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
Hey All,

This may be a simple one but I thought I would throw it out there
because I can't find an answer. Structures are value types as we know.
Thus they are stored on the stack. What happenes though, if you have a
struct that contains a ref type such as a string? Is the struct still
a value type? Or is it now stored in heap space?

Brette.Net

Aug 1 '07 #2
On Aug 1, 1:36 pm, Brette....@gmail.com wrote:
This may be a simple one but I thought I would throw it out there
because I can't find an answer. Structures are value types as we know.
Thus they are stored on the stack.
No - that's a myth. The struct will be stored on the stack *if* it's a
local variable, or an instance variable of another value on the stack.
If it's an instance variable of a class, the value will be on the
heap.
What happenes though, if you have a
struct that contains a ref type such as a string? Is the struct still
a value type? Or is it now stored in heap space?
The struct is still a value type, and it contains a reference.

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

Jon

Aug 1 '07 #3
On Aug 1, 8:20 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 1, 1:36 pm, Brette....@gmail.com wrote:
This may be a simple one but I thought I would throw it out there
because I can't find an answer. Structures are value types as we know.
Thus they are stored on the stack.

No - that's a myth. The struct will be stored on the stack *if* it's a
local variable, or an instance variable of another value on the stack.
If it's an instance variable of a class, the value will be on the
heap.
What happenes though, if you have a
struct that contains a ref type such as a string? Is the struct still
a value type? Or is it now stored in heap space?

The struct is still a value type, and it contains a reference.

Seehttp://pobox.com/~skeet/csharp/memory.html

Jon
Great that makes sense!

Brette.Net

Aug 1 '07 #4

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

Similar topics

7
by: Michael Birkmose | last post by:
Hi, Using gcc the following is possible: int some_function(struct local_struct { int member;} a); This function takes one parameter "a" of the type struct local_struct. This type is...
30
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
2
by: S. Lorétan | last post by:
Hello. I have some structs in different namespaces/classes/other structs and I sometime have to check if it contains something or not. myStruct == null doesn't work. I've currently done it...
5
by: giddy | last post by:
hi , i'm a C / C# programmer .. have'nt done C++, in C# .. . object instances of classes ( TextBox txt = new TextBox();) are reference types. Structs on the other hand are value types. In...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.