Tuesday, August 20, 2019 ..:: Home ::.. Register  Login

Peter Henry on Facebook  Peter Henry on LinkedIn  Peter Henry on Twitter

All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
   TechTidBits (Blog)  

How do you bake constants into your code?

Sep 21

Written by:
Sunday, September 21, 2008 3:03 PM  RssIcon

Do you use magic number/strings in your code?  Shame on you!  Do you use constants or read only variables, KUDOS to you!  But do you know the difference between a const and a readonly variable?  Read on for some insights into their differences.

Since the beginning of programming coders have been using magic strings/numbers in their code.  But about six months after releasing their code (and returning from vacation) they couldn't remember WTF they were thinking and what those "magic" values meant.  They were magical all right!

Fast forward to relatively modern languages (and I'm sure some older ones too :>) and you have constants support in the compiler.  The classic example is PI = 3.14, the developer would dictate in their code that PI = 3.14 and from now on, anything using that piece of code understands what PI is.  Beautiful.  THAT is magic IMHO.  But what happens if you want your variable to act like a constant but you don't know what it is at compile time?  hhhmmm  What's a coder to do in that case?  Most, if not all modern languages have the concept of a read only variable for just such an occasion.  The quick explanation is a read only variable acts like a constant in that you can't change it, but the one BIG difference is your able to set it at runtime.

Here is a fast/terse list of differences between the two (and some error messages if you break the rules).


  • automatically considered static, therefore don't need that scope modifier, in fact the compile will complain about it

Constants cannot be declared with static keyword

  • value is initialized at COMPILE time therefore the compiler MUST be able to figure out the value otherwise an error results

Constants cannot be declared with static keyword

  • cannot be changed once it's assigned, that's just something you can't do, sort of like disobeying the laws of gravity. 

Constants cannot be declared with static keyword

  • must be a value type, nope, cannot put any classed into a const cause that would mean your initializing with a new and therefore it's done at runtime but we already discussed, const variables are initialized at compile time.

Constants cannot be declared with static keyword

Read Only

  • can be either instance or static variables

Constants cannot be declared with static keyword

  • evaluated at runtime so you can have a variable act like a const but initialized at runtime, this is the best of both worlds

Constants cannot be declared with static keyword

  • Although you can get the best of both worlds, you are limited to where you can initialize the variable

Constants cannot be declared with static keyword


I hope you enjoyed this brief discussion on the differences between const and readonly.  There are benefits to both of these keywords and now I hope you're better armed to make up your own mind on which to use in your code.


References: const at MDSN, readonly at MSDN, Patrick Steele's blog entry, CSharp-Online.NET blog entry


Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement