'How to import functions from an external namespace onto my project in c sharp

I want to import functions from a different namespace in an external file into my c sharp project. But what's the right method to do it. I copied the .cs file which contains the namespace into my project directory but "using " keyword showing error even the files are from the same directory. To be precise i'm trying to import api functions provided by a company for a hardware which are inside a different .cs file. What's the right way to do it?



Solution 1:[1]

Basically, you could create a new project with the same namespace as your imported .cs files have and add it as a project reference. Another method is to modify the namespace in the imported files to match the used namespace in your project.

It really depends on the structure and logic you're importing, personally, I would go with option 1, and maybe even create some interfaces for them, to expose only the functionality I would need to use from them (Facade pattern).

Solution 2:[2]

Try to right click the project which you want to add.

Then click on "Add => Existing item...".

Select the .cs file you want and click "Add button".

Now you can using "the .cs file namespace" in your project.

Solution 3:[3]

You can add it to your project reference.

Run the below in your terminal.

dotnet add reference [library.csproj]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Bucsko
Solution 2 大陸北方網友
Solution 3 Suraj Rao