Alex Rivera | Logout

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

Asked 2010-09-23T17:06:31.553
70

I have an internal class, an internal constructor won't allow it to be used in a generic collection so I changed it to public. What's the accessibility if you have a public constructor in an internal class and is it any different to having an internal constructor?

c#
Edit
Report

1 Answer

65

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.

answered 2010-09-23T17:15:05.430

Your Answer