如上所述,它是多余的,从对象继承在C#? 难道这两套code以下结果等效对象被定义?
As stated above, is it redundant to inherit from Object in c#? Do both sets of code below result in equivalent objects being defined?
class TestClassUno : Object
{
// Stuff
}
VS
class TestClassDos
{
// Stuff
}
我四处探听MSDN上,但未能找到任何确凿的完美
I snooped around on MSDN but wasn't able to find anything perfectly conclusive.
如果不指定每类
定义将隐式地从 System.Object的因此,这两个定义是等价的。
If left unspecified every class
definition will implicitly inherit from System.Object
hence the two definitions are equivalent.
这两个是不同的唯一的一次是,如果有人真的定义的另一个对象
键入同一个命名空间。在这种情况下对象的局部定义
将采取precedence和更改继承对象
The only time these two would be different is if someone actually defined another Object
type in the same namespace. In this case the local definition of Object
would take precedence and change the inheritance object
namespace Example {
class Object { }
class C : Object { }
}
非常多的一个角落里的情况下,但如果我之前
Very much a corner case but wouldn't point it out if I hadn't seen it before
请注意,同样是不正确的,如果你使用对象
而不是对象
。 C#的关键字对象
是一个类型别名 System.Object的
,因此它不会匹配 Example.Object
。
Note that the same is not true if you used object
instead of Object
. The C# keyword object
is a type alias for System.Object
and hence it wouldn't match Example.Object
.
namespace Example2 {
class Object { }
class C : Object { } // Uses Example.Object
class D : object { } // Uses System.Object
}
当然,如果你有一个真正的邪恶的开发人员,你仍然会造成混乱对象
namespace System {
class Object {
private Object() { }
}
}
namespace Example3 {
// This will properly fail to compile since it can't bind to the private
// Object constructor. This demonstrates that we are using our definition
// of Object instead of mscorlib's
class C : object { } // Uses our System.Object
}