October 8

Project References in C#

Let how to add a project reference in C# using Visual Studio 2017. Learn how the new csproj syntax greatly simplify how to work with project references.


Hi, I'm Andrea Angella, and welcome to the Practical C# Sharp video series. In this video, I want to talk about Project References in C#. I'm using Visual Studio 2017 Community Edition, and I have a solution with two projects. So application, practical C#, and a library called "My Library." I want to show you how you can reference your library, "My Library," from the application, and actually use it using Visual Studio. So let's do that.

The way to project reference to a project related to, click on "References", right click on "Reference" inside Projects Solution, you can see the list of projects, and you should check this one and you press "okay." As you can see now, My Library is listed under references, and now you can use any types provided by your library inside your solution. In this case, my library provide a StringUtils plus that I can use directly here, to count the words of a text. Let's say I love C#, that's fine, let's see if it works. Excellent! So now, Microsoft application is using My Library to perform some particular task. And this is actually how you add a project reference to a project. But it is very important to understand what actually happened under the covers so that when you deal with project conflicts, you can understand how to solve them. I put all this code into our repository, so all the changes we've made since the beginning of the video are now recorded. And basically when you explore it, you can see what are the changes that happened.

Of course, I've changed the programme, but also you can see that Visual Studio automatically change the practical C# csproj file. If you double click on it, you can see that this file contains a lot of stuff about your project, and Visual Studio added an item group here, and in particular a project reference tag. This project reference won't actually make this project to reference your library. As you can see, include provides a way pass to the csproj of your library. Then you need to provide a guid of the project of the library and the name of the library. And this guid is actually the project ID that is actually present inside this file here that is included.

If you go back in the solution of My Library, you need to do "Undo Project" first, and then you can do "Edit My Library" csproj, and you can see that here I have a project guid. So this project guid must match the value that we entered inside the project reference tag. It's a bit convoluted. But this is actually how so far you log project references in project. Fortunately, Visual Studio does it for you, but it is important to understand what happened under the covers.

Now, what I want to tell you, since this is 2017 has been released, Microsoft simplified greatly how C# proj files can be created. And I want to show you that, because also the syntax in order to add the project reference changed. So let me reload My Library, and now I want to edit and show you how to do this. If I unload my console application project and edit it, I want to show you that we don't need this anymore, you don't need to provide any of this. You only need to provide the Sdk that you are target. In our case we are going to target .NET.sdk. We don't need import, we only need the target framework. See this is slightly different, we have v4.6.2, we need to do .net462, like that. Everything can be removed. And let me keep the reference, but we don't even need the guid anymore, we don't even need the name of the project, so I just take project reference, and then provide the location of the cs project you want to reference. So look at this, it's amazing. So it's incredibly simpler now.

All the C# files are automatically added inside of your projects by default. And the only thing you need to do is to provide the target framework and include any project reference you care about. That's it. Let's reload it and see what happened. So reload it and try to build, and we got some errors. We got duplicate system reflection assembly company attribute, configuration attribute, and so on. Another thing that Microsoft did is automatically add the assembly info for you. So this means that you don't need AssemblyInfo file anymore. Now if I build, you don't see any conflicts. And if you look under what happened here, References have been renamed to Dependencies, and your project dependencies under "Projects" here. And you can see your MyLibrary reference is still there.

This is basically how you can add dependencies in Visual Studio. Obviously we did it manualy, if I remove the dependency here, and I edit the file, look at this, this is important. I don't have to undo the project to do an edit, I can just do edit directly. This is another improvement that has been made. See, now the dependencies have gone... If I want to edit it in Visual Studio, this is the process: Right click on "Dependencies," "Add reference," select your reference, you do "Okay," and then let's see what our Visual Studio added for you. The same thing we added manually.

So, now from Visual Studio 2017, you can add your project references in a much simpler way. Unfortunately, there is no simple tool that allows you to convert the old C# syntax with the new one. You can do it manually fairly easily as demonstrated in this video. When you create a new .NET Core or .NET Standard project, the new templates already provide the new syntax. Put importantly if you create a full .NET framework projects, you still need to use the old syntax.

This is it, this is how you can add project references in your files, and keep in mind that from 2017, the new syntax is a lot simpler and you can avoid a lot of pain and conflicts when you deal with C# projects going forward. If you like the video, please share it with your friends and coworkers, and if you want to learn more about C# and stay up to date with the net, and get a notification every time I release a new video, please subscribe to the channel.


You may also like

Stepping Down as .NET Cambridge Community Leader

Stepping Down as .NET Cambridge Community Leader

Top 10 C# Developers Videos from .NET Conf 2021

Top 10 C# Developers Videos from .NET Conf 2021
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Use this Bottom Section to Promote Your Offer

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim