Sumitra Paul

Sumitra Paul

  • NA
  • 51
  • 33.6k

Problem in understanding compite time and runtime constant

Dec 22 2013 2:17 AM
Hi,

I know the basic differences between const and readonly.
My doubt is on one specific paragraph from a book. I am wrting down the paragraph.
If posible please help understanding me (I have marked the line with "****(Not understood)" which i havenot understood.):

paragraph from book:

The way in which compile time and runtime constants are evaluated affects runtime compatibility.
Suppose you have defined both const and readonly fields in a assembly named infrastructure:

    public class Usefulvalues
    {
        public static readonly int Starvalue = 5;
        public const int EndValue = 10;
    }

In another assembly, you reference these values:

    for(int i = UsefulValues.StartValue; i < UsefulValue.EndValue; i++)
        Console.Writeline(i);

We get the output as 5, 6, ....9

 Time passes and you release a new version of the Infrustructure assembly with the following changes:

    public class Usefulvalues
    {
        public static readonly int Starvalue = 105;
        public const int EndValue = 120;
    }

****(Not understood) You distrubute the Infrustructure assembly without rebuilding your Application assembly. You expect to get

    105, 106, ...119

In fact you get no output at all. The loop now uses value 105 for its start and 10 for its end condition. The C# compiler
placed the const value of 10 into the application assembly instead of a reference to the storage used by endvalue.Contrast
that with the startvalue. It was declared as readonly: It gets resolved at runtime.

*** (Not understood) Therefore the application assembly makes use of the new value without even recompiling the application assembly. Simply installing an updated version of the Infrustructure assembly
is enough to change the behavior of all clients using that value.

The above text from the book says "You distrubute the Infrustructure assembly without rebuilding your Application assembly". Now I don't understand this because as per my knowledge without rebuilding your assembly how can you distribute it to the client.

Also what is the difference between Infrustructure assembly as mentioned in the example code and the Application assembly ??


Thanks.

Answers (3)