This question already has an answer here:
I've seen some C# code that declares a class with an internal
modifier, with a public
constructor:
internal class SomeClass
{
public SomeClass()
{
}
}
What is the point of having a public constructor, if the visibility of the entire class is internal, thus it can be seen only inside the defining assembly?
Also, does this make any sense in case SomeClass is a nested class?
The
internal class
scope overrides thepublic MyClass()
constructor scope, making the constructorinternal
.Using
public
on the constructor makes it easier to update the class topublic
later, but confuses intent. I don't do it.Edit 3: I missed part of your question. It is still fine to do that if your class is nested. The nesting can't make any difference, even if it is nested in a private class in a public class in a ... (see C# language specification - 3.5.2 Accessibility domains).
EDIT: And, if i recall, if the ctor is
internal
, it can't be used as a generic type where there is a constraint requiringwhere T : new()
, this would require apublic
constructor (ref. C# language specification (version 4.0) - 4.4.3 Bound and unbound types).Edit 2: Code sample demonstrating the above
From MSDN - Access Modifiers (C# Programming Guide):
So if, for example, you have an internal implementation of a public interface, you can still expose certain members as public.
Additionally, suppose you suddenly want your internal class to be public. It's a lot easier simply to change the access modifier on the class than all of the members.
The internal keyword is an access modifier for types and type members. Internal members are accessible only within files in the same assembly.
Example: Microsoft internal modifier