什么是在一个内部类的公共构造的意思 [英] What does a public constructor on an internal class mean

查看:154
本文介绍了什么是在一个内部类的公共构造的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些C#code声明一个类的内部修改,使用公开构造器:

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?

此外,这是否有什么意义的情况下的 SomeClass的的是一个嵌套类?

Also, does this make any sense in case SomeClass is a nested class?

推荐答案

内部类范围覆盖公共MyClass的()构造范围,使得构造内部

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.

编辑3:我错过了你的问题的一部分。它仍然是细做,如果你的类嵌套。嵌套不能作出任何的区别,即使是嵌套在一个私有类的公共类的吧...(见的 C#语言规范 - 3.5.2辅助域的)

编辑:,然后,如果我还记得,如果构造函数是内部,它不能作为一种通用型哪里有需要约束其中T:新的(),这将需要一个公开构造函数(REF的 C#语言规范(4.0版) - 4.4.3绑定和非绑定类型

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).

编辑2: code样品展示上述

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>();
    }
}

这篇关于什么是在一个内部类的公共构造的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆