1

I'm trying to figure a way to have a static class with constant strings that doesn't force recompilation when values from this class changes.

I've read in this highly voted comment that:

... if you change a (const) value you must also recompile all assemblies dependent on your assembly defining the constants - therefore its often safer to go the readonly route.

So I did a short experiment to check if it's true.

I have 4 projects: Program, ProjectA, ProjectB & ProjectC. Each "ProjectX" has a single class "X" while program stores main.

In C I have a single public const string someString = "some const string", which is referenced by a public method in B, which in turn is referenced by a public method in A, and A's method is called by Program.Main.

Whether I make someString a const, a static readonly, a getter only expression bodied property or a getter only property with a property initializer, all the projects get rebuilt no matter what.

So is it true that I can have a static class that holds readonly string values that won't propagate recompilations to all depending projects once it's value is changed?

asaf92
  • 1,557
  • 1
  • 19
  • 30
  • 4
    The build system looks at dependencies, not at semantics. So if project A is rebuilt and project B references it, it will also be rebuild, even it it would work also without rebuild. – Klaus Gütter Nov 04 '20 at 12:00
  • So why does it matter if it's a const or not from a developer stand point? You're gonna have everything recompile anyways. – asaf92 Nov 04 '20 at 12:04
  • 1
    The projects might not be built together. Think of a library A and a third-party application using it. Then you want to avoid any breaking change in the library forcing the other developers to rebuild. – Klaus Gütter Nov 04 '20 at 12:07
  • OK, so in 3rd party library case it makes sense, but when using a monolith solution with references between projects? Is it still common practice to use these large static classes with lots of `const string`s, or are people using other solutions (such as resource files)? – asaf92 Nov 04 '20 at 12:13
  • Related: https://stackoverflow.com/a/35575516/736079 – jessehouwing Nov 04 '20 at 15:13

1 Answers1

1

There are two different issues here:

const means the value treated as a compile time literal. I.e. each time it is used the compiler will insert the actual literal value where it was used. This can be an issue if you dynamically load dlls, or use another version of a dll than it was compiled against. Read more about const vs static readonly.

The problem you are describing is the build dependency system. I.e. when doing an incremental build it will rebuild all modified projects and all projects that depend on a project that will be rebuilt. This behavior has nothing to do with const vs static readonly. Inserting a space in a code file is sufficient for the project to be considered modified.

If you only have four projects it is probably easiest just to live with it. If it is an actually a problem you could move the values to a configuration file so they can be changed without recompiling anything.

JonasH
  • 28,608
  • 2
  • 10
  • 23