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.
Loading
VulpesPosted Dec 23, 2013, 2:22 PM
I tested it using a command line build.
I started by creating this source code file in notepad:
and then compiled it to a dll with this line:
csc /t:library infrastructure.cs
I then created this source code file in notepad:
and then compiled it to an exe with this line:
csc /r:infrastructure.dll application.cs
When I ran application.exe, the output as expected was:
I then changed infrastructure.cs to this:
and recomplied it with this line, thereby overwriting the original infrastructure.dll:
csc /t:library infrastructure.cs
I then ran application.exe again without recompiling it and - as expected - there was no output at all.
However, when I did recompile application.cs and ran it, the output was - again. as expected:
Sumitra PaulPosted Dec 23, 2013, 12:45 PM
Thanks for replying. I always wait for your answer as I found them the most precise.
But as I am little new to c# mostly on deloyment of applications. I have some doubt :-(
I have understood what you have explained but how to test this locally.
I thought of trying it.
I created a class library named "classLibrary1" and put those codes:
public class Usefulvalues
{
public static readonly int Starvalue = 5;
public const int EndValue = 10;
}
Also created another console application and added this library and written below code.
for(int i = UsefulValues.StartValue; i < UsefulValue.EndValue; i++)
Console.Writeline(i);
It printed 5,6...9.
Now I changed the value in ClassLibrary1 by
public class Usefulvalues
{
public static readonly int Starvalue = 105;
public const int EndValue = 120;
}
Now I added classLibrary1 to the console application and run the console application
in non debugging mode hoping that I will get nothing. But I got the result 105,106...
I am very much sure you didn mean what I did with the lines mentioned below.
"When the second version of the dll is produced, it's distributed to client applications but without an instruction to rebuild the source code of those applications against the new version of the dll."
As far as I know I will build my dll and send it to the client. Client will straight away add my dll and use it. Do the client again need to build the application ?
Can you plz tell me how can I test this locally ??
Thanks again.
Regards
Sumitra
VulpesPosted Dec 23, 2013, 6:02 AM