Home > Net >  Converting a C# class I created inside a project to a separate reusable class
Converting a C# class I created inside a project to a separate reusable class

Time:01-25

I'm not new to C# programming, but I suppose I'm new to programing "the right way" in C#. I've worked in C on embedded devices for years and have written desktop apps to support them. First in VB6, then in C#.

I recently started making better use of classes for reusing code (and for instantiating more than one instance of the class in a program). For example, I "wrapped" a UART interface with some additional functionality so I can use the same code for multiple ports by creating an instance of the class for each one.

It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.

I'm sure there's a way to create it such that I can just reference it like everything else with either a "using..." reference at the top of the program or with a "Project | References..." checkbox. But for the life of me I can't find a good learning journey for this.

Any direction would help.

CodePudding user response:

You want to create your reuseable class in an assembly - this is the equivalent of a dll from your C experience.

To create an assembly, have a separate project of type assembly (instead of exe) . You can reference the assembly from other projects. If your project is in the same solution you can reference the project, otherwise you can reference the compiled assembly.

C# uses a packaging system called Nuget, so you can package your assemblies into "Nugets" which you host in a Nuget Server. You can then use tooling to discover and import these.

CodePudding user response:

It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.

Well, it isn't the best practice but (unfortunatly) still a common behavior. So don't worry to much about it.

What you could do to improve it place the file (and other reusable parts) in a seperated csproj. For example name the project of the type class library and name it VinDag.Tools. Within the project create a folder UART and place the wrapper there. The namespace of the wrapper would then be VinDag.Tools.UART.

From know on you can just reference the class library instead of renaming the file. It's not necessarily required to be the same namespace as the project.

From there you can start considering (private) nugets. This would prevent you from copying files/csproj around.

CodePudding user response:

Please create a Class Library project and include your class into that project. Make sure your class is public. Once you build this project you'll get an assembly which can be referenced from other projects. See Tutorial: Create a .NET class library using Visual Studio

There are different ways of referencing it.

  1. You can have the class library project in the same solution as the main project. In this case you should add a project reference.
  2. You can copy the compiled *.dll file to some folder in your solution (e.g. Lib) and add an assembly reference.
  3. If this assembly is to be used in multiple projects please consider creating a NuGet package with this library and pushing it to some repository. Then other projects can add a package reference to this package.

Details:

  • Related