什么是在一个内部类中的公共构造和内部构造的区别? [英] What's the difference between a public constructor in an internal class and an internal constructor?

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

问题描述

我有一个内部类,内部构造也不会允许它在泛型集合中使用,所以我把它改成公开。什么是辅助功能,如果你有在一个内部类中的公共构造函数,并为它有什么不同,以具有内部构造?

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?

推荐答案

二是基本相同的。我见过他们的区别的一个说法是,让你的构造内部确保将只通过类型在当前组件内被实例化的类型,即使是后来决定该类型本身应该是公共而不是内部。换句话说,你可能会决定更改的类型的的知名度不解除对限制其的实例

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.

此外,正如你已经指出的,人们的的区别是,如果构造函数的内部,类型不能被用作新的()约束,因为这个约束要求的构造函数是公共,其中T泛型类型参数$ C>。

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.

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

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