一切都在.NET对象? [英] Is everything in .NET an object?

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

问题描述

请帮我们解决的的几乎一切都是对象的争议的(<一个href=\"http://stackoverflow.com/questions/436079/as-a-novice-is-there-anything-i-should-beware-of-before-learning-c#436092\">an回答堆栈溢出问题的作为一个新手,有什么学习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

  • 不是一切从派生
    基本类型的对象? - rizzle

  • 大多数事情都是对象... - 奥马尔
    Kooheji

  • 值类型,整型,双打,对象
    引用(未对象他们
    自我)等不是对象。他们可以
    被盒装看起来像物体(例如
    i.ToString()),但实际上他们
    基本类型。更改进入
    几乎全部的寄托是一个对象和
    我会删除downvote - 二进制
    杞人忧天

  • 我AP preciate澄清。一世
    认为的最低水平,你可以
    互动,说一个int,在C#是
    作为一个结构,它是不是一个对象? -
    <一href=\"http://msdn.microsoft.com/en-us/library/ms173109.aspx\">http://msdn.microsoft.com/en-us/library/ms173109.aspx
    - rizzle

  • 不的Int32自ValueType继承
    从对象继承?如果是这样,
    尽管行为,一个int是
    目的。克里斯 - 农民

  • 不,盒装类型INT继承
    自ValueType,从继承
    目的。他们不是在对象
    传统意义上因为:a)一个int
    是不是为int的引用,这是
    整型。 B)int的数据是不是垃圾
    集。如果声明一个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的

  • 打开类型参数的类型也没有从 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.

MSDN条目System.Object的:

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

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所有类
  框架是从Object派生,
  在对象定义的每个方法
  类是在中的所有对象提供
  系统。派生类可以做
  覆盖了其中一些方法。

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的导出。即使对于那些类型,你仍然需要注意引用类型之间的差异< A HREF =htt​​p://msdn.microsoft.com/en-us/library/s1ax56ch.aspx>值类型的,因为他们的待遇却截然不同。

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的,他们的处理方式不同从内存中引用类型,以及他们是如何的语义通过在你的code方法传递的不同的。事实上,值类型不被视为一个对象(引用类型),直到你明确地指示您的应用程序通过拳击它作为引用类型这样做。请参见关于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天全站免登陆