I've made a package for the first time that I've been using in a project of mine, and the intended use is MyPackage.Parse(), but currently it has to be used as MyPackage.MyPackage.Parse().
The package is split into two projects, MyPackage and MyPackage.Tests. MyPackage has a class file called again, MyPackage, which looks like
namespace MyPackage
{
public static class MyPackage
{
public static PackageModel Parse(string text)
{
}
}
}
I'm guessing I'm getting my problem because Parse is in a static MyPackage class, which is in a MyPackage namespace.
The user also has access to my other folders and files that I'm not really interested in showing them, like MyPackage.Constants, MyPackage.Services.
So how do I create a NuGet package where the end result is they can just use MyPackage.Parse()?
CodePudding user response:
Namespaces are not mandatory, so you could just put your class in the implicit global namespace. Then, clients do not need to qualify your class with a namespace.
However, please do not do that. At the moment, all your clients need to do is just adding a simple using MyPackage; at the top of their files in order to achieve what you intend. This is good practice.
Namespaces are there to avoid name conflicts and when you don't use them, you are at risk of name conflicts. What is worse, your clients are also at risk of name conflicts. If they were using another package that has a class named MyPackage, they will hardly be able to use your class any more, only through ugly syntax constructs. Therefore, the convention is use namespaces, even if that means that your clients have to add a using statement.
