Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a public constructor on an internal class mean [duplicate]

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?

like image 943
lysergic-acid Avatar asked May 11 '13 18:05

lysergic-acid


People also ask

Can a constructor be internal?

Instance constructorInstance constructors can have public, private, protected, external, or internal modifiers.

Can you have a public constructor for a private class?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.

When would you use an internal constructor?

internal constructor are good when you don't want the client to create the instance. You'd usually want to do it if you want to control how the instance should be created.

Can constructor be internal in C#?

Constructors can be marked as public, private, protected, internal, protected internal or private protected. These access modifiers define how users of the class can construct the class. For more information, see Access Modifiers. A constructor can be declared static by using the static keyword.


2 Answers

The internal class scope overrides the public MyClass() constructor scope, making the constructor internal.

Using public on the constructor makes it easier to update the class to public 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 requiring where T : new(), this would require a public constructor (ref. C# language specification (version 4.0) - 4.4.3 Bound and unbound types).

Edit 2: Code sample demonstrating the above

class Program
{
    internal class InternalClass {
        internal InternalClass() { }
    }
    internal class InternalClassPublicCtor {
        public InternalClassPublicCtor() { }        
    }
    internal class GenericClass<T>
        where T : new() {}

    static void Main(string[] args) {
        GenericClass<InternalClass> doesNotCompile = new GenericClass<InternalClass>();
        GenericClass<InternalClassPublicCtor> doesCompile = new GenericClass<InternalClassPublicCtor>();
    }
}
like image 191
Andy Brown Avatar answered Oct 07 '22 01:10

Andy Brown


From MSDN - Access Modifiers (C# Programming Guide):

Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

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.

like image 31
Ant P Avatar answered Oct 07 '22 00:10

Ant P