C# .NET 中的一切都是对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/436211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is everything in .NET an object?
提问by missaghi
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 Hymanass" (This American Life).
请帮助我们解决“几乎”一切都是对象的争议(堆栈溢出问题的答案作为新手,在学习C#之前我应该注意什么?)。我认为情况就是这样,因为 Visual Studio 中的所有内容至少都显示为一个结构体。请发布参考,以免它成为“现代蠢货”(This American Life)。
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).
请注意,这个问题是指 C#,不一定是 .NET,以及它如何处理幕后数据(显然都是 1 和 0)。
Here are the comments to "everything is an object":
以下是对“一切都是对象”的评论:
- Eh, no, it's not. – Binary Worrier
- I'd like an example... – scotty2012
- isn't everything derived from the base type Object? – rizzle
- Most things are objects... – Omar Kooheji
- Value types, ints, doubles, object references (not the objects them selves) etc aren't objects. They can be "boxed" to look like objects (e.g. i.ToString()) but really they're primitive types. Change the entry to "NEARLY everthing is an object" and I'll remove the downvote – Binary Worrier
- I appreciate the clarification. I think the lowest level that you can interact with, say an int, in C# is as a struct, which isn't an object? - http://msdn.microsoft.com/en-us/library/ms173109.aspx– rizzle
- Doesn't Int32 inherit from ValueType which inherits from Object? If so, despite the behavior, an int is an object. – Chris Farmer
- No, the boxed type for int inherits from ValueType, which inherits from Object. They're not objects in the traditional sense because a) an int isn't a reference to an int, IT IS the int. b) ints aren't garbage collected. If you declare an Int32, then that int is 4 bytes on the stack, end of story – Binary Worrier
- 呃,不,不是。– 二元担忧
- 我想要一个例子... – scotty2012
- 不是所有的东西都是从基本类型 Object 派生的吗?– 细雨
- 大多数东西都是对象...... – Omar Kooheji
- 值类型、整数、双精度数、对象引用(不是它们本身的对象)等都不是对象。它们可以被“装箱”以看起来像对象(egiToString()),但实际上它们是原始类型。将条目更改为“几乎所有事物都是对象”,我将删除不赞成票 – Binary Worrier
- 我很欣赏澄清。我认为你可以在 C# 中交互的最低级别,比如 int,是作为一个结构,它不是一个对象?- http://msdn.microsoft.com/en-us/library/ms173109.aspx- rizzle
- Int32 不是从继承自 Object 的 ValueType 继承的吗?如果是这样,不管行为如何,int 都是一个对象。– 克里斯·法默
- 不,int 的盒装类型继承自 ValueType,后者继承自 Object。它们不是传统意义上的对象,因为 a) int 不是对 int 的引用,它是 int。b) 整数不是垃圾收集的。如果你声明一个 Int32,那么这个 int 是栈上的 4 个字节,故事结束 – Binary Worrier
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."
对象的定义:“对象”作为类 System.Object 的继承者,“对象”作为类型的实例,“对象”作为引用类型。
采纳答案by Daniel Schaffer
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".
这里的问题是这实际上是两个问题——一个是关于继承的问题,在这种情况下答案是“几乎所有”,另一个是关于引用类型与值类型/内存/装箱,在这种情况下答案是“不”。
Inheritance:
遗产:
In C#, the following is true:
在 C# 中,以下是正确的:
- 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 toSystem.Object
, but interfaces only derive from other interface types, andSystem.Object
is not an interface type. - No pointer types derive from
System.Object
, nor are any of them directly convertible toSystem.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
. - 所有类、数组和委托类型都派生自
System.Object
. - 接口类型不是从
System.Object
. 它们都可以转换为System.Object
,但接口仅派生自其他接口类型,而System.Object
不是接口类型。 - 没有指针类型派生自
System.Object
,也不能直接转换为System.Object
。 - “开放”类型参数类型也不是从
System.Object
. 类型参数类型不是从任何东西派生的;类型参数被限制为从有效基类派生,但它们本身不是从任何“派生”的。
From the MSDN entry for System.Object:
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.
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.
支持 .NET Framework 类层次结构中的所有类,并为派生类提供低级服务。这是 .NET Framework 中所有类的最终基类;它是类型层次结构的根。
语言通常不需要类来声明从 Object 继承,因为继承是隐式的。
由于 .NET Framework 中的所有类都是从 Object 派生的,因此 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 typesand value types, as they are treated very differently.
因此,并非 C# 中的所有类型都源自System.Object
. 即使对于那些类型,您仍然需要注意引用类型和值类型之间的区别,因为它们的处理方式非常不同。
Boxing:
拳击:
While value types do inheritfrom 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.
虽然值类型确实继承自System.Object
,但它们在内存中的处理方式与引用类型不同,并且它们如何通过代码中的方法传递的语义也不同。实际上,值类型不会被视为对象(引用类型),除非您通过将其装箱为引用类型来明确指示应用程序这样做。在此处查看有关 C# 中装箱的更多信息。
回答by Maxime Rouiller
Based on all books that I read, everything in C# is an object.
根据我读过的所有书籍,C# 中的一切都是对象。
Some are reference other are Value type. Value type object inherit from the class ValueType. They have different behavior but inherently ... objects.
有些是参考其他是值类型。值类型对象继承自ValueType类。它们具有不同的行为,但本质上是......对象。
This is the reason why you can store an Int32 in an object variable as well as everything that you can ever create in .NET.
这就是为什么您可以将 Int32 以及您可以在 .NET 中创建的所有内容存储在对象变量中的原因。
For more detail... look at the following: http://msdn.microsoft.com/en-us/library/s1ax56ch(VS.71).aspx
有关更多详细信息...请查看以下内容:http: //msdn.microsoft.com/en-us/library/s1ax56ch(VS.71).aspx
All value types are derived implicitly from the Object class.
所有值类型都是从 Object 类隐式派生的。
回答by Ron Warholic
Value types are not objects, they obey different copying semantics, different passing semantics, and must be wrapped in a class (Object) in order to be treated as such.
值类型不是对象,它们遵循不同的复制语义、不同的传递语义,并且必须包装在一个类(Object)中才能被如此对待。
Edit: I think the argument is somewhat vague, as you must qualify what you mean by 'object'. Is an object just something that inherits from Object, or is it something that obeys Object's use semantics? Or are we talking the most general definition of object, where it is anything that can contain data and operations on that data?
编辑:我认为这个论点有些含糊,因为您必须限定“对象”的含义。对象只是从 Object 继承的东西,还是遵循 Object 的使用语义的东西?或者我们在谈论对象的最一般定义,它是任何可以包含数据和对该数据进行操作的东西?
回答by Joe Phillips
The number 2 is not an object.
数字 2 不是对象。
回答by BBetances
You're confusing an object with a value or reference. Basically, everything is an object. An Int is an object, but it is also a value type. A class instance is an object, but it is also a reference type.
您将对象与值或引用混淆了。基本上,一切都是对象。Int 是一个对象,但它也是一种值类型。类实例是一个对象,但它也是一个引用类型。
Methods aren't objects, nor are properties. The just operate on objects. And yes, pretty much everything inherits from the object class.
方法不是对象,也不是属性。只是对对象进行操作。是的,几乎所有东西都继承自对象类。
回答by Rich
I thought that value types are NOT objects. They're stored differently in memory by the CLR - value types are stored on the stack, and objects are stored on the heap. You can cast value types to a reference type to make them act like an object, but the CLR takes the value off of the stack, wraps it in an object, and stores it on the heap. That is what happens when you "box" a variable.
我认为值类型不是对象。它们由 CLR 以不同的方式存储在内存中 - 值类型存储在堆栈中,而对象存储在堆中。您可以将值类型转换为引用类型,使它们像对象一样运行,但 CLR 会从堆栈中取出值,将其包装在一个对象中,然后将其存储在堆中。这就是当您“装箱”一个变量时会发生的情况。
回答by GEOCHET
They are all treated asobjects, but they are not all objects. The confusion comes in with Autoboxing.
它们都被视为对象,但它们并不都是对象。混乱来自于自动装箱。
See this for more information: http://en.wikipedia.org/wiki/Object_type
有关更多信息,请参阅:http: //en.wikipedia.org/wiki/Object_type
The abstraction confuses people apparently.
这种抽象显然使人们感到困惑。
回答by Sunny Milenov
In C# (and in OOP in general) we have types (class - reference, struct - value, etc.). These are the definitions. And the "object" is the concrete instance of a given type.
在 C#(以及一般的 OOP)中,我们有类型(类 - 引用、结构 - 值等)。这些是定义。而“对象”是给定类型的具体实例。
So, if we read the question literally, yes, everything is an object when instantiated.
所以,如果我们从字面上理解这个问题,是的,当实例化时,一切都是对象。
The confusion most probably begins with a bad choosing of the name of the very base class for everything. In .NETthis is the Object class.
混乱很可能始于对所有内容的基类名称的错误选择。在.NET 中,这是 Object 类。
回答by Konrad Rudolph
Some people here have a strange notion of what an “object” in object-oriented programming is. In order for something to be an object it does nothave to be a reference type or, more generally, follow any formal implementation.
这里有些人对面向对象编程中的“对象”是什么有一个奇怪的概念。为了让事情成为一个对象时,它并没有必须是引用类型,或者更一般地说,按照任何正式实施。
All that means is that you can operate on it as a first-class citizen in an object-oriented world. Since you cando this on values in C# (thanks to autoboxing), everything is indeed an object. To some extend, this is even true for functions (but arguably not for classes).
这意味着您可以在面向对象的世界中作为一等公民来操作它。由于您可以在 C# 中对值执行此操作(感谢自动装箱),因此一切确实都是一个对象。在某种程度上,这甚至适用于函数(但可以说不适用于类)。
Whether this is relevant in practice is another question but this is a general problem with OOP that I notice once again. Nobody is clear on the definition of OOP (yes, most people agree that it has something to do with polymorphism, inheritance and encapsulation, some throw in “abstraction” for good measure).
这在实践中是否相关是另一个问题,但这是我再次注意到的 OOP 的一个普遍问题。没有人清楚 OOP 的定义(是的,大多数人都同意它与多态性、继承和封装有关,有些人为了更好的衡量而加入了“抽象”)。
From a usage point of view, every value in C# handles like an object. That said, I like the currently accepted answer. It offers both technically important aspects.
从使用的角度来看,C# 中的每个值都像一个对象一样处理。也就是说,我喜欢目前接受的答案。它提供了技术上重要的两个方面。
Notice that in other contexts, e.g. C++, other aspects are stressed since C++ isn't necessarily object-oriented and furthermore is much more focused on low-level aspects. Therefore, the distinction between objects, POD and builtin primitives makes sometimes sense (then again, sometimes not).
请注意,在其他上下文中,例如 C++,强调其他方面,因为 C++ 不一定是面向对象的,而且更侧重于低级方面。因此,对象、POD 和内置原语之间的区别有时是有意义的(再说一次,有时不是)。
回答by Gavin Miller
From: Value Types (C# Reference) - MSDN 3.5
All value types are derived implicitly from the System.ValueType.
所有值类型都是从 System.ValueType 隐式派生的。
From: Value Type Class - MSDN 3.5
ValueType overrides the virtual methods from Object with more appropriate implementations for value types.
ValueType 用更合适的值类型实现覆盖了 Object 的虚方法。
From: Enum Class - MSDN 3.5
This class inherits from ValueType
这个类继承自 ValueType
The Inheritance Hierarchy is as follows:
继承层次结构如下:
- System.Object
- System.ValueType
- System.Enum
- System.ValueType
- 系统对象
- 系统值类型
- 系统枚举
- 系统值类型
Conclusion: Everything is an object
结论:一切都是对象