I’m building a library where I’ve got a pattern like the following:
public class Foo : Foo_Impl
{
}
public class Foo_Impl
{
}
I don’t want other developers to accidentally use the Foo_Impl class. What options are available to me to hide this? I’d also like to hide it from other classes in the same assembly it’s defined in. Ideally I’d have loved to do:
public class Foo : Foo_Impl
{
private class Foo_Impl
{
}
}
But that doesn’t work for a variety of reasons.
Make
Foo_Implan abstract class. This won’t stop other developers from deriving from it, but it will make it impossible to create an instance ofFoo_Impldirectly – it will need to be instantiated by creating a derived object, such asFoo.–
Per Thomas Levesque’s suggestion, you also have the option of making the abstract constructor internal:
This will prevent developers from inheriting from
Foo_Implfrom outside the assembly.