I've created two executable projects which depend on a single class library.
The class library contains only classes and constants (no variables or functions), and is used to store application-level protocol information for communication between the two executable projects.
When testing my two programs, I notice that they run without needing the class library (DLL) located in the same directory.
What is happening here? Do I need to distribute the DLL if only constants are used from it?
If they are genuine constants, introduced with const
, then the value of each constant will be inlined where it's used. For example, if you have:
// Assembly1
public static class Constants
{
public const string Foo = "Hello";
}
// Assembly2
public class Test
{
static void Main()
{
Console.WriteLine(Constants.Foo);
}
}
... then the value "Hello" will be inlined into Assembly2 and you don't need Assembly1 to be present. No code will refer to it, even if it's still listed as a reference within Assembly2.
However, if you ever access the constants by reflection (which is entirely reasonable) then you would want to have Assembly1 present.
See more on this question at Stackoverflow