Friday, July 6, 2012

c# readonly vs constant in different assembly

In C#/.NET we can declare a constant value by either using keyword 'const' or 'readonly' . Using const keyword will define compile time constant and readonly will define runtime. Only the C# built-in types can be declared using 'const' for user-defined types like class, struct or array use 'readonly'. Compiler will have a literal value for all the fields that declared const, so if you decompile the code you will find no reference to the constant but actual value. Compile time constant are faster then readonly but are less flexible and can create issues if not used properly. As a general rule one should strictly use compile time constant only for values that are never going to change for example defining value of PI, any value that might change in future use readonly.

During developing a large application there are numerous scenario where you might have to choose between compile-time and run-time constant's. Compile time constants are faster then run-time, although in certain conditions to avoid potential problems you might want to consider using run-time constants . The difference between the two is more clearly explained in this post [ linky ]

Here is an practical example, two teams are working on a same project one team develops a external class library and other team develops the main application. Team one has developed the class library that has some const and readonly variables, these values are consumed in the application developed by the team two. If in future team one updates the constant value in the external class library and the application is not recompiled it would not reflect the new value. This issue is only created if your are using constant values form external assemblies. If a const value changes in a assembly then you need to rebuild all the clients applications dependent on it.

   1:  using System;
   2:  using ExternalLibrary;
   4:  namespace ExternalLibrary
   5:  {
   6:      public class ConstantLib
   7:      {
   8:          public static readonly int StartValue = 105;
   9:          public const int EndValue = 120;
  10:          public readonly int ReadonlyValue = 555;
  11:      }
  12:  }
  16:  namespace CTvsRT
  17:  {
  18:     class Program
  19:     {
  20:        static void Main(string[] args)
  21:        {
  22:         ConstantLib cl = new ConstantLib();
  24:         Console.WriteLine("ConstantLib.StartValue {0}", ConstantLib.StartValue.ToString());
  25:         Console.WriteLine("ConstantLib.EndValue {0}", ConstantLib.EndValue.ToString());
  26:         Console.WriteLine("ConstantLib.readonlyValue {0}", cl.ReadonlyValue.ToString());
  28:         Console.Read();
  29:        }
  30:     }
  31:  }

No comments:

Post a Comment