Friday, October 22, 2004

VJs Tip Of The Day - October 22nd 2004

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% gurantee for the information to be correct... I welcome any debates over the topic and would be glad to 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 sturcture 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 charcters 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 existance of strctures... It is also probably the ease that C++ developers whould 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 moreever 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...

PS: What a struggle it is to adopt to a new Version Control System; this can be briefly explained by someone like me in atleast 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...



No comments: