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;
3:
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: }
13:
14:
15:
16: namespace CTvsRT
17: {
18: class Program
19: {
20: static void Main(string[] args)
21: {
22: ConstantLib cl = new ConstantLib();
23:
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());
27:
28: Console.Read();
29: }
30: }
31: }
No comments:
Post a Comment