C#System.Object是最终的基类 [英] C# System.Object being the ultimate base class

查看:55
本文介绍了C#System.Object是最终的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在msdn规范中,我可以注意到 System.Object 是.Net中的最终基类.他们说 System.ValueType 是从 System.Object 继承的抽象类,并覆盖了 Equals Compare 等方法.值类型,例如 bool int 等.都从 System.ValueType 继承,所有其他.net对象都从 System.Object .

In the msdn spec, I could notice that System.Object is the ultimate base class in .Net. They say that System.ValueType is an abstract class inheriting from System.Object and overrides the methods like Equals, Compare etc... Value types like bool, int etc.. inherit from System.ValueType all the other .net objects inherits from System.Object.

我对此有2个问题.

  1. System.Object有什么需要?为什么在这里没有首选接口?

Am假设它只有两个直接子代(忽略我们可以创建更多子代),它们是System.ValueType和System.ReferenceType都具有完全不同的实现.

****没有System.ReferenceType.只有Sytem.Object和Sytem.ValueType(覆盖基类).抱歉.

所以可能需要System.Object来处理基本的CLR功能,例如使用new()创建对象,强制执行默认构造函数,GC等?

Am assuming it has only 2 direct Children(ignoring that we can create more) which is System.ValueType and System.ReferenceType both having entirely different implementations.

****There is no System.ReferenceType. There is only Sytem.Object and Sytem.ValueType (overriding the base class). Apologies here.

So System.Object may be needed to handle the basic CLR functionalities like object creation using new(), enforcing default constructor, GC etc ?

  1. 当我反编译 Sytem dll并看到bool的实现时,我只看到一个结构.
    对于一个类(例如Exception),我看不到对System的继承..ReferenceType或System.Object.如何处理继承?
    实际上,Common Type System对 MyCustomClass 进行什么操作以使其继承于 System.Object (因为继承是在编译时确定的,所以我认为CTS正在这样做))
  1. When I de-compile Sytem dll, and see the implementation of bool, I just see a struct.
    For a class (say Exception) I don't see the inheritance to System.ReferenceType or System.Object. How is this inheritance handled ?
    Infact, what is Common Type System doing to MyCustomClass to make it inherit from System.Object(since the inheritance is determined at compile time am thinking CTS is doing it)

如果我的理解有误,请随时纠正我/编辑帖子.

Please feel free to correct me/edit the post if my understanding is wrong.

推荐答案

您在此问题中有很多问题.以后,考虑每个问题发布一个问题.

You have many questions in this question. In the future, consider posting one question per question.

System.Object有什么需要?

What is the need of System.Object?

问题很模糊.让我改一下.

The question is vague. Let me rephrase.

为什么要设计C#类型系统以使所有非指针类型都具有通用基类型?

Why was the C# type system designed so that all non-pointer types had a common base type?

获得多态性的好处.在没有泛型的类型系统(例如C#1.0类型系统)中,您希望能够列出任意对象.

To gain the benefits of polymorphism. In a type system that has no generics, like the C# 1.0 type system, you want to be able to make a list of arbitrary objects.

为什么接口不比类类型更受青睐?

Why wasn't an interface preferred over a class type?

接口指定功能.基类允许共享实现.Object上的方法具有在派生类型之间共享的实现.

Interfaces specify capabilities. Base classes allow for shared implementation. The methods on Object have implementations that are shared amongst the derived types.

Am假设它只有两个直接子代(忽略我们可以创建更多子代),它们是System.ValueType和System.ReferenceType都具有完全不同的实现.

Am assuming it has only 2 direct Children(ignoring that we can create more) which is System.ValueType and System.ReferenceType both having entirely different implementations.

这完全是错误的.

因此可能需要System.Object来处理基本的CLR功能,例如使用new()创建对象

So System.Object may be needed to handle the basic CLR functionalities like object creation using new()

否.

强制使用默认构造函数

enforcing default constructor

不.虽然最终会调用对象的默认构造函数是正确的,但该构造函数不会执行任何操作.因此说这种类型的目的是确保调用不执行任何操作的构造函数是一件很奇怪的事情.

No. Though it is true that ultimately the default constructor of object is called, that constructor doesn't do anything. So saying that the purpose of the type is to ensure that a constructor that doesn't do anything is called is a strange thing to say.

GC等?<​​/p>

GC etc ?

否.

当我反编译System.dll并看到bool的实现时,我只看到一个结构.

When I de-compile System.dll, and see the implementation of bool, I just see a struct.

正确.您应该知道所有非空,非枚举结构都直接从System.ValueType继承.

Correct. You are expected to know that all non-nullable, non-enum structs inherit directly from System.ValueType.

对于一个类(例如Exception),我看不到对System.ReferenceType或System.Object的继承.

For a class (say Exception) I don't see the inheritance to System.ReferenceType or System.Object.

正确.您应该知道所有类类型都从System.Object继承(当然,如果未指定其他类型).

Correct. You are expected to know that all class types inherit from System.Object (if no other type is specified, of course.)

如何处理此继承?

How is this inheritance handled ?

这个问题太模糊了,无法回答.

The question is too vague to answer.

Common Type System对MyCustomClass进行什么操作以使其从System.Object继承(因为继承是在编译时确定的,因此我认为CTS会这样做)

what is Common Type System doing to MyCustomClass to make it inherit from System.Object(since the inheritance is determined at compile time am thinking CTS is doing it)

我绝对不知道您要在这里问什么.尝试使问题更精确.

I have absolutely no idea what you're trying to ask here. Try making the question more precise.

您没有问过但可能应该问过的问题是:

A question you did not ask but probably should have asked is:

我说类型A从类型B继承是什么意思?

What does it mean when I say that type A inherits from type B?

这意味着所有B型成员也都是A型成员.

It means that all members of type B are also members of type A.

所有成员?甚至是私人会员?

All members? Even private members?

是的.继承是一种类型的成员同时也是另一种类型的成员的属性.

Yes. Inheritance is the property that the members of one type are also members of another.

这篇关于C#System.Object是最终的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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