I have 3 project : P1, P2 and P3
In P1, there is a definition of an Interface I
In P2, there is a Class C that implements Interface I
public class C : I
{
...
}
In P3, I have that method in a class:
public static C getC (int id)
{
...
}
At compile time, P3 does not compile because interface I is defined in a project that is not referenced. Why?
Note that I make no use of the interface in P3, I use the concrete class.
The reason is because the interface
Iis part of the public signature ofC, thus when you useCit has to know about the interface definition as well. Thus it requires the assembly to be referenced.To illustrate:
Because
Iis publicly visible as a part ofC(for example, you can assign aCto a reference of typeI, etc), assembly 3 that wants to use C needs both the definition ofC(assembly 2) andI(assembly 1)However, if
CusedIinternally (not visible to other assemblies), you wouldn’t need to referenceI‘s assembly to useC. For example:Does that make sense? Because a class’s base classes and interfaces are part of its definition, they need to be known by any referencing assemblies. But if they are used internally but not visible externally, then they do not necessarily need to be known by any referencing assemblies.