What’s the difference between a public constructor in an internal class and an internal constructor?

The two are essentially the same. One argument I’ve seen for distinguishing between them is that making your constructor internal ensures the type will only ever be instantiated by types within the current assembly, even if it is later decided that the type itself should be public instead of internal. In other words you could decide to change the type’s visibility without lifting restrictions on its instantiation.

Making the constructor public has the opposite effect (obviously), and might be sensible if you want for it to be possible to instantiate the type anywhere it is visible.

Also, as you’ve already pointed out, one small difference is that if the constructor’s internal, the type cannot be used as a generic type argument for a generic type with a where T : new() constraint, as this constraint requires for the constructor to be public.

Leave a Comment

tech