.NET 中的一切都是对象吗? [英] Is everything in .NET an object?

查看:38
本文介绍了.NET 中的一切都是对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我们解决几乎"一切都是对象的争议(对堆栈溢出问题的回答作为新手,在学习 C# 之前我应该​​注意什么?).我认为情况就是这样,因为 Visual Studio 中的所有内容至少都显示为一个结构体.请发布参考,以免它成为现代傻瓜"(美国生活).

Please help us settle the controversy of "Nearly" everything is an object (an answer to Stack Overflow question As a novice, is there anything I should beware of before learning C#?). I thought that was the case as everything in Visual Studio at least appears as a struct. Please post a reference, so that it doesn't become "modern jackass" (This American Life).

请注意,这个问题是指 C#,不一定是 .NET,以及它如何处理幕后数据(显然都是 1 和 0).

Note that this question refers to C#, not necessarily .NET, and how it handles the data under the hood (obviously it's all 1's and 0's).

以下是对一切都是对象"的评论:

Here are the comments to "everything is an object":

  • 呃,不,不是.– 二元担忧
  • 我想要一个例子... – scotty2012
  • 不是所有的东西都来源于基本类型对象?– 嘶嘶声
  • 大多数东西都是对象... – Omar库赫吉
  • 值类型、整数、双精度数、对象引用(不是它们的对象自己)等不是对象.他们能被装箱"以看起来像物体(例如i.ToString()) 但实际上它们是原始类型.将条目更改为几乎所有的东西都是一个对象"和我将删除downvote – Binary担心
  • 感谢您的澄清.一世认为你能做到的最低水平与 C# 中的 int 进行交互是作为一个结构,它不是一个对象?——http://msdn.microsoft.com/en-us/library/ms173109.aspx– 嘶嘶声
  • Int32 不是从 ValueType 继承的哪个继承自Object?如果是这样,尽管有这种行为,但 int 是目的.– 克里斯·法默
  • 不,int 的盒装类型继承来自 ValueType,它继承自目的.他们不是对象传统意义上,因为 a) 一个 int不是对 int 的引用,它是内部结构b) 整数不是垃圾集.如果你声明一个 Int32,那么那个 int 是 4 个字节堆栈,故事的结尾——二元担忧

对象的定义:对象"作为类 System.Object 的继承者,对象"作为类型的实例,对象"作为引用类型."

Definition of object: "Object" as a inheritor of class System.Object vs. "object" as an instance of a type vs. "object" as a reference type."

推荐答案

这里的问题是,这实际上是两个问题——一个是关于继承的问题,在这种情况下,答案是几乎所有",另一个是关于引用类型 vs 值类型/内存/装箱,在这种情况下答案是否定的.

The problem here is that this is really two questions - one question is about inheritance, in which case the answer is "nearly everything", and the other is about reference type vs value type/memory/boxing, which case the answer is "no".

继承:

在 C# 中,以下是正确的:

In C#, the following is true:

  • 所有值类型,包括枚举和可为空的类型,都是从 System.Object 派生的.
  • 所有类、数组和委托类型均派生自 System.Object.
  • 接口类型不是从 System.Object 派生的.它们都可以转换为System.Object,但接口只派生自其他接口类型,System.Object 不是接口类型.
  • 没有从 System.Object 派生的指针类型,它们中的任何一个也不能直接转换为 System.Object.
  • Open"类型参数类型也不是从 System.Object 派生的.类型参数类型不是从任何东西派生的;类型参数被限制为从有效基类派生,但它们本身并不从任何派生".
  • All value types, including enums and nullable types, are derived from System.Object.
  • All class, array, and delegate types are derived from System.Object.
  • Interface types are not derived from System.Object. They are all convertible to System.Object, but interfaces only derive from other interface types, and System.Object is not an interface type.
  • No pointer types derive from System.Object, nor are any of them directly convertible to System.Object.
  • "Open" type parameter types are also not derived from System.Object. Type parameter types are not derived from anything; type arguments are constrained to be derived from the effective base class, but they themselves are not "derived" from anything.

来自 System.Object 的 MSDN 条目:

支持 .NET 中的所有类框架类层次结构并提供派生类的低级服务.这是所有的最终基类.NET 框架中的类;它是类型层次结构的根.

Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.

语言通常不需要声明继承自的类对象,因为继承是隐含的.

Languages typically do not require a class to declare inheritance from Object because the inheritance is implicit.

因为 .NET 中的所有类框架派生自对象,对象中定义的每个方法类在所有对象中可用系统.派生类可以并且可以做覆盖其中一些方法.

Because all classes in the .NET Framework are derived from Object, every method defined in the Object class is available in all objects in the system. Derived classes can and do override some of these methods.

因此,并非 C# 中的所有类型都源自 System.Object.即使对于那些类型,您仍然需要注意 引用类型之间的区别值类型,因为它们的处理方式非常不同.

So not every type in C# is derived from System.Object. And even for those types that are, you still need to note the difference between reference types and value types, as they are treated very differently.

拳击:

虽然值类型从 System.Object继承,但它们在内存中的处理方式与引用类型不同,以及它们如何通过代码中的方法传递的语义也不同.实际上,值类型不会被视为对象(引用类型),除非您通过将其装箱为引用类型来明确指示应用程序这样做.请参阅更多关于 C# 中的拳击的信息.

While value types do inherit from System.Object, they are treated differently in memory from reference types, and the semantics of how they are passed through methods in your code are different as well. Indeed, a value type is not treated as an Object (a reference type), until you explicitly instruct your application to do so by boxing it as a reference type. See more information about boxing in C# here.

这篇关于.NET 中的一切都是对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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