This is a mirror of official site: http://jasper-net.blogspot.com/

Hidden Facts of C# Structures in terms of MSIL

| Sunday, October 17, 2010
Did you ever thought of a program without using a single structure declared in it ?  Never thought so na? I did that already but found it would be impossible to do that, at least it does not make sense as the whole program is internally running on messages passed from one module to another in terms of structures.

Well, well  hold on. If you are thinking that I am talking about custom structures, you are then gone too far around. Actually I am just talking about declaration of ValueTypes. Yes, if you want to do a calculation, by any means you need an integer, and System.Int32 is actually a structure. So for my own program which runs without a single ValueType declared in it turns out to be a sequence of Print statements. Does'nt makes sense huh! Really, even I thought so. Thus I found it is meaningless to have a program which just calls a few library methods (taking away the fact that library methods can also create ValueType inside it) and prints arbitrary strings on the output window.

Please Note : As struct is actually an unit of ValueType, I have used it synonymously in the post.

So what makes us use Structures so often?

The first and foremost reason why we use structures is that
They are very simple with fast accessibility.
It is also created on Thread local stack, which makes it available only to the current scope and destroyed automatically after the call is returned. It doesn't need any external invisible body to clear up the memory allocated henceforth.
Most of the primitives have already been declared as struct.
In case of struct we are dealing with the object itself, rather than with the reference of it.
Implicit and explicit operators work great with struct and most of them are included already to the API.

Read more: Dot Net Tricks

Posted via email from .NET Info

0 comments: