-1

I have an assembly with a class having const string variable,

public class Data {     
     public const string Version = "1.0.0";
}

This will be used in another assembly to assign to a string property,

 public class ViewModel 
 {
     AppVersion = Data.Version;

     public String AppVersion 
     {
         get;
         set;
     }
 }

This AppVersion will displayed on UI application.

Problem is, I have updated this Version to 1.0.1 and built only this assembly has Data class and moved the dll to production.

But, this new version is not displaying and shows old version (1.0.0) still.

When I built the other assembly which has AppVersion (ViewModel) then the new version displayed.

What was the issue? How my assembly keeps the olded version value?

Meu
  • 17
  • 4
  • 1
    `Version` is marked with `const` ? And please show the code where you are actually updating the version. – Dimitar Apr 04 '19 at 05:34
  • Ignoring the const inlining issue, you probably want to use the assembly file version instead? https://stackoverflow.com/a/909583/2441442 – Christian Gollhardt Apr 04 '19 at 06:29

1 Answers1

0

This is a key difference between const and static readonly. Just change you Data class like this and you will be fine:

public class Data {     
    public static readonly string Version = "1.0.0";
}

Explanation:

At compile time of AppVersion = Data.Version, your compiler will see that Data.Version is const string so it just replace this line with AppVersion = "1.0.0" (for the optimization sake).

On the other hand, when you Data.Version is static readonly string compiler will know that he (or she? Roslyn) need a reference to Data type to get this static field value, so when you switch you Data dll - your ViewModel dll will see an actual value.

vasily.sib
  • 3,871
  • 2
  • 23
  • 26