什么公共构造在C#抽象类的工具? [英] Whats the utility of public constructors in abstract classes in C#?
问题描述
如果在一个抽象类中的公共构造函数只能由他们的派生类中调用它应该是功能上等同于一个受保护的构造。对吧?
If a public constructor in an abstract class can only be called by their derived classes it should be functionally equivalent to a protected constructor. Right?
有没有在声明的公共构造任何区别,而不是一个受保护的,在抽象类?你会用它来做什么?为什么编译器不投诉?
Is there any difference in declaring a public constructor, instead of a protected one, in an abstract class? What would you use it for? Why the compiler doesn't complaint?
感谢
推荐答案
绝对正确。你应该有利于保护构造
Absolutely correct. You should favor the protected constructor.
编辑:没有编译器不抱怨,但喜欢的FxCop(安培;代码分析)工具做。我相信有一些奇怪的反射技巧可以与抽象类公共构造做的,而是从一个角度来看,你仅仅是提供了基类的功能,其他开发人员编写的子类,坚持与保护的构造。
no the compiler doesn't complain, but tools like FxCop (& Code Analysis) do. I believe there are some weird reflection tricks you can do with public constructors on abstract classes, but from a standpoint where you are merely providing base class functionality to other developers writing subclasses, stick with the protected constructor.
这篇关于什么公共构造在C#抽象类的工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!