Reputation: 43
Trying to understand where all of the dependencies of projects in a visual Studio c# solution are defined. (Edit: This is in .NetCore 3.1)
For projects in a solution dependent on other projects in the same solution, the dependency appears to be defined in the dependent project in two ways:
Yet for projects that are dependent on a NuGet package which is itself a dependent of a different project in the same solution, the dependency on the NuGet package appears to be only be explicitly defined one way:
There does not appear to be any explicit dependency of the dependent project on any other project. Yet it is dependent.
A good example of this is NLog usage where NLog is defined in some init project then simple namespace references allow any project in the solution to use it.
I’ve been trying to reuse projects from other solutions and ran into this. Yes, it’s easy to catch but I’d like to understand how this all works.
My question is this: what is the rule for this type of implicit dependency? Does the nuget package have to be defined in some project in the hierarchy of projects that depend on the project using the package? Is it just some solution-level configuration I’ve missed? And why are they different?
I’ve searched and haven’t been able to locate anything that explains how but maybe I missed it. Thanks and apologies if this is a stupid question
Upvotes: 3
Views: 1582
Reputation: 13842
There are really two things.
First your VS project references some nuget packages and some VS projects. A nuget package contains one or several assemblies and a VS projects outputs an assembly. This means that in-fine you tell the compiler which assemblies to reference at compile time.
Second the using
statements in your C# file is just a facility to avoid writing the full type name.
System.Console.Writeline("Hello world!");
vs.
using System;
using ABC;
...
Console.Writeline("Hello world!");
Notice that the compiler is searching Console
among:
as long as the type name
System.
ABC.
If several such type are found (like System.Console
and ABC.Console
) the compiler emits an ambiguous error.
Upvotes: 3
Reputation: 1071
I'll try to go in order of the process:
A nuget package only defines where the library comes from (generally online from nuget.org, but you can add variety of sources), and then visual studio downloads that package into the "Packages" folder
If you added a project via the Nuget package manager, the project itself then references that DLL in the packages folder. If you expand the "References" section in your project and double click on a reference, you can see where the actual library is located.
The Using statement simply imports that namespace into the .CS file you're working in so you don't have to specify the entire path to an object direct (e.g. System.IO.File...).
Upvotes: 1