New Message on dotNET User Group Hyd

VJs Tip Of The Day

Reply
  Reply to Sender   Recommend Message 65 in Discussion
From: VishalRJoshi

Structures - A Discussion

Today's tip is not a tip but is a discussion... Yesterday I had a discussion
with a friend of mine over Structures and their presence in .Net... Here are
the abstracts of our discussion... Below mentioned are educated guesses and
I do not claim 100% guarantee for the information to be correct... I welcome
any debates over the topic and would be glad to learn more in the process...
I shall update the related discussion on the blog, along with
acknowledgements, in time to come...

A simple question!!... Should we ever use Structures in managed .Net
development at all? and if so then when?...

Here are some of the thoughts...
We all know that a structure is a value type and a class is a reference
type, other than this what else, how does it help us answer our questions...
Well, we should also know that inheritance is not possible with structure so
anyways for such class specific uses we should never use structures... As
structure is a value type, it is an obvious thought that structures will be
faster but is it really true?
Consider the following... You are using a structure with 4 properties; the
data type for all these properties is string. Now string is a reference type
so where does string go... Structure is a value type and it resides on the
stack; string is a reference type it should reside on the heap, right!!...
We should agree that string will not get automatically unboxed to get into
stack and it is not possible also...Now if all your properties go and stay
on the heap because they are reference type then where is the advantage of
struct being faster??
So few of the situations where you could use structures over classes is when
you know all the internal datatypes to the structure are value types... If
you know that you have stuff like just integers and characters in a class
then you can as well have it as a structure and prevent GC from getting
burdened with collection of the class...
Other instance where you could use structures is when your interoperability
demands it and that is probably the primary reasons for existence of
structures... It is also probably the ease that C++ developers would have
coming to .Net...
We should also care to think that Structure is sequential data type and so
the data is allocated as it comes, there is no management with the memory
done here... In case of class as it is on the heap and so the memory can be
managed, this can ensure that proper compression and alignment of memory
happens which is not controlled in structures... But as we talked earlier
the drawback with classes is that even though your object might no more be
used you are not sure when GC will come and clear it and moreover it is also
more work for the background thread...
Well these are some of the thoughts... There were more but they would lead
to firecrackers so am holding on to them.... You guys are welcome to post in
your comments and ideas...

Vishal Joshi
Microsoft MVP .Net
If You Think YOU CAN... You Can...
http://VishalJoshi.Blogspot.com
http://www.microsoft.com/india/mvp/indiamvp.aspx
http://groups.msn.com/ChennaiNetUserGroup
http://groups.msn.com/CNUG-DAM
http://groups.msn.com/NetBloomingtonUserGroup

PS: What a struggle it is to adopt to a new Version Control System; this can
be briefly explained by someone like me in at least over 2000 words essay...
In just past 1 year I have used VSS, RMS, Dimensions, StarTeam, Sourcegear
Vault and I don't know what not... As usual I messed up this time too... My
work for over 1 week was swallowed by the Version Control just as if it was
a magician and I was its sole spectator... As usual again I kept looking
onto the screen and saying "It can't be... common it must be somewhere...
search the machine... Ohhhh no no, it can't be... " Well these Version
Control Systems are irreversible magicians in my case... Many came tried
something and finally said to me, you are dumb, how could you do that... and
then again... "Oh!! it can't be..."; But you know what, I just realized that
it's a Friday today... I will think about this crap on Monday... This time I
really need to go to the "TGI Friday", the name 'Thank God It's Friday'
really means something to me today...


View other groups in this category.

baazee.com

Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to