Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
What are the differences? Pros and cons? When to use one over the other?

 

Well.....

 

the difference between structures and classes is that instances of structures are VALUE types and classes are REFERENCE types.

 

what does this mean in practice ....

 

structure

dim test as strnew
dim test2 as strnew
test.one = 5
test2.one = 6

test.one = test2.one
'test.one = 6 and test2.one = 6
test2.one = 8
'test.one = 6 and test2.one = 8

 

Classes

dim test as classnew
dim test2 as classnew
test.one = 5
test2.one = 6

test.one = test2.one
'test.one = 6 and test2.one = 6
test2.one = 8
'test.one = 8 and test2.one = 8   !!!

 

the reason this happens is because you have reference types when you work with classes.... these types refer to a memory location.... so you copy the reference and not the value

 

(I haven't tested the code above...Correct me if I am wrong....but that should be the basic idea... structures - value types / classes - reference types)

 

INTER

Posted

test.one = test2.one

'test.one = 6 and test2.one = 6

test2.one = 8

'test.one = 8 and test2.one = 8 !!!

That wouldn't happen, because .one (which looks like an integer/short/etc.) is a value type, therefore test.one is stored in test and test2.one is stored in test2.

 

If it was a reference type, such as a StringBuilder (though strings, which are reference types, behave like value types in this situation because they are immutable) then the behavior would be similar to what you showed above.

 

In addition, classes must be initialized before use, the same is not true for structs.

"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted (edited)
No background evidence given for the < 16 bytes. I think he made it up.
LOL. The first time I heard that I thought the same thing - why? You'll see the 16 byte rule mentioned over and over. My question is, why is that?

 

Not that I have any evidence myself, I've never bothered to perf it out. Sometimes you hear something so many times from people that are smarter than you, you just take it for granted.

 

Here's a better explanation, along with a disclaimer of course. Scroll down to "Data Size".

 

Mike

 

Edit: Allow me to point out that the above link mentions that there is a trade off between copying the bytes of a value (structure) and allocating a new reference on the heap (class). That does make sense.

Edited by mhildner
Posted

Just a point of interest, performance is a real issue with my CAD system, I have spent many an our deliberating over what perform better. The day I tried out a performance profile (I use ANTS profiler) it was a revelation, I kid you not, with in two hours I saw a 15 fold increase in speed in a key area. You can hone in on bottle necks, you will be surprised what sort of thing cause performance issues.

If you are serious about performance, you need a profiler.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...