C#: My experience with const keyword

Csharp
Csharp

C#: My experience with const keyword

Welcome to CodeSpread!

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.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

2 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their ...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • SQL:My first SSIS PackageSQL:My first SSIS PackageI would like to share my experience about a situation, where I was given a task to export a file(say XML) from FTP location and get the data from this file populated into our database tables. Moreo...