C# Fundamentals Part 2 (class vs. struct)

M

Marcel Hug

Hi Ng !
At first I would like to thank you about the very good answers on my C#
Fundamental question one.
Now I have a new question. It's about structs.

So I've read that the different between a class and a strct is that a
class is an reference type and a struct a value type.
For me that means, that the CLR places the data of a struct on the stack
and not on the heapo like for a class.
But what does happen, if I create a struct that has data members of a
reference type ? I guess that the variable (data member) of this
reference type is just holding a pointer, which points on the heap !
It's that right ? Is it recommendet to use reference types in structs ?

Thanks for helping me to improve my understanding !
Regards
Marcel Hug
 
M

Mattias Sjögren

Marcel,
For me that means, that the CLR places the data of a struct on the stack
and not on the heapo like for a class.

That's only true for local variables.

But what does happen, if I create a struct that has data members of a
reference type ? I guess that the variable (data member) of this
reference type is just holding a pointer, which points on the heap !
It's that right ?
Yes


Is it recommendet to use reference types in structs ?

Well there's nothing wrong with it.


Mattias
 
M

Michael Nemtsev

Hello MArcel,

Jon Skeet open this topic in his article here: http://www.yoda.arachsys.com/csharp/memory.html

MH> Hi Ng !
MH> At first I would like to thank you about the very good answers on my
MH> C#
MH> Fundamental question one.
MH> Now I have a new question. It's about structs.
MH> So I've read that the different between a class and a strct is that
MH> a
MH> class is an reference type and a struct a value type.
MH> For me that means, that the CLR places the data of a struct on the
MH> stack
MH> and not on the heapo like for a class.
MH> But what does happen, if I create a struct that has data members of
MH> a
MH> reference type ? I guess that the variable (data member) of this
MH> reference type is just holding a pointer, which points on the heap !
MH> It's that right ? Is it recommendet to use reference types in
MH> structs ?
MH> Thanks for helping me to improve my understanding !
MH> Regards
MH> Marcel Hug
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top