How to Use a C# Class Library in a Project

How do I use a C# Class Library in a project?

Add a reference to it in your project and a using clause at the top of the CS file where you want to use it.

Adding a reference:

  1. In Visual Studio, click Project, and then Add Reference.
  2. Click the Browse tab and locate the DLL you want to add a reference to.

    NOTE: Apparently using Browse is bad form if the DLL you want to use is in the same project. Instead, right-click the Project and then click Add Reference, then select the appropriate class from the Project tab:

    Sample Image
  3. Click OK.

Adding a using clause:

Add "using [namespace];" to the CS file where you want to reference your library. So, if the library you want to reference has a namespace called MyLibrary, add the following to the CS file:

using MyLibrary;

How do I get a reference to a class library project to work?

In order to be able to reference a library from a .NET framework project the libary needs to target .NET Framework or .NET Standard.

.NET 5 and .NET Framework are different beasts.

A good spot to see this is .NET Standard article:. You can see there that .NET and .NET Framework are separate rows in the table showing .NET versions.

Sample Image

.NET Standard 2.0

The answer to why "this persists for every .NET target framework except for .NET Standard 2.0" can be found in the in the same article:

To find the highest version of .NET Standard that you can target, do the following steps:

  • (...)
  • Repeat this process for each platform you want to target. If you have more than one target platform, you should pick the smaller version among them. For example, if you want to run on .NET Framework 4.8 and .NET 5.0, the highest .NET Standard version you can use is .NET Standard 2.0.

4.8 and 5.0. It's confusing.

Yes it can be confusing

In Introducing .NET 5 from May 2019 Richard Lander, program manager in .NET Team, writes:

.NET 5 = .NET Core vNext

.NET 5 is the next step forward with .NET Core. (...)
The project aims to improve .NET in a few key ways:

  • Produce a single .NET runtime and framework that can be used everywhere and that has uniform runtime behaviors and developer experiences.
  • Expand the capabilities of .NET by taking the best of .NET Core, .NET Framework, Xamarin and Mono.
  • (...)

Later he writes:

We’re skipping the version 4 because it would confuse users that are familiar with the .NET Framework, which has been using the 4.x series for a long time. Additionally, we wanted to clearly communicate that .NET 5 is the future for the .NET platform.

And finally:

Check out .NET Core is the Future of .NET to understand how .NET 5 relates to .NET Framework.

Using classes in Class library project

System.Drawing is a Windows only component and as such requires .NET Framework (or a compatibility pack).

If you want to use .NET Standard, you can add the System.Drawing.Common Nuget Package to your project.

Add C# class library reference to VB.Net Project

Assuming the dll in question is CLS compliant and compiled against the same runtime version, you should be able to use it without a problem.

If either (or both) of these conditions are not met, you will not be able to use the imported DLL.

Make sure the Import directive uses the namespace as defined in the assembly metadata - look at your C# project properties to see what the default namespace is, that's what you need to import.

Handle Class Library Project Through a Windows Form Project

Do not expose form controls to "outside world", instead provide public method which can be called by form consumers.

Inside method you can update you control.

In library project

public class Form1
{
public void UdpateTextBoxWith(string newText)
{
textbox1.Text = newText;
}
}

In Winforms application

var form = new ClassLibTestProject.Form1();
form.Show();

form.UdpateTextBoxWith("New text");

How to use a C# class in many projects inside the same solution

Create a Class Library project within your solution and put your shared classes in there (right click on your solution in the Solution Explorer, Add, New Project). Then for each of the projects that you want to access the classes from, right click on Dependencies, Add Project Reference and tick the Class Library project you created.



Related Topics



Leave a reply



Submit