C#: My experience with const keyword
There are numerous post on internet about ‘what is const keyword in .net’ or ‘what is the difference between const keyword and read only’ and I must say many might have already mugged it up like me
I can bet, many of us might not have tried some exercise to learn the real implementation or did not get caught in a situation where the learning would have come on the doorsteps by itself. I was lucky as the learning came in a disguise to me but unlucky because we have to bang our head for sometime as a crucial deliverable deadline was approaching.
Lets begin with the scene of a ODC where team is working during the UAT week. As per the changes received, DDLs containing helper classes needs to be changed and updated in the environment. Our main website project contains only the reference to the helper classes so there was no need to build the website project. It’s a normal procedure and we have carried it out several times so we were sure everything will work. But, we were about to learn something and there it was, all the changes that were asked were available on the website but one const field which was changed in the DLL is showing the old value only. We checked the helper class and find that it is updated correctly then why our website is showing old value.
On banging our head for quite sometime, we build our website project also and to our surprise, we got the updated value. Great! We realized that website will certainly keep the references to DLLs and update its IL-intermediate language code, but if we look at the IL for a const field then we will find the actual value and not the const variable name like :
For const a=5, IL will not contain variable name,it will only contain the actual value 5.
In our case, our website IL contained the actual value so on building the DLLs, references got updated but not the actual value. To update it, Website was also required to be build.
Real time experiences can teach us insightful lessons and provide meaning to our learning. Please share yours.