如果结构不能继承另一个类或结构,为什么Int32有一个ToString()方法? [重复]

时间:2021-10-04 16:10:26

This question already has an answer here:

这个问题在这里已有答案:

int a = 2;

Console.WriteLine(a.ToString()); // displays 2

// definition of ToString() here - public override string ToString();

Now, here are some of my understandings:

现在,以下是我的一些理解:

  1. All the classes in .net get a ToString() method, which is inherited from the Object class.
  2. .net中的所有类都获得一个ToString()方法,该方法继承自Object类。
  3. A structure cannot be derived from a Class or another struct. int is a structure of type Int32, which gets a couple of ToString() [With Parameters] methods from the Interfaces which it implements.
  4. 结构不能从Class或其他结构派生。 int是Int32类型的结构,它从它实现的接口获取几个ToString()[With Parameters]方法。
  5. There is also a ToString() [without params] function in struct Int32
  6. struct Int32中还有一个ToString()[没有params]函数

According to http://msdn.microsoft.com/en-us/library/system.int32.tostring.aspx,

根据http://msdn.microsoft.com/en-us/library/system.int32.tostring.aspx,

struct Int32 overrides ValueType.ToString() method

struct Int32重写ValueType.ToString()方法

If a struct cannot inherit some class or struct, can you please explain how this ToString() method is available for Int32?

如果结构不能继承某个类或结构,那么请解释一下这个ToString()方法如何用于Int32?

3 个解决方案

#1


30  

If a struct cannot inherit some class or struct,

如果结构不能继承某个类或结构,

This isn't true. All structs (and the built-in value types, like System.Int32, System.Single, etc) always implicitly inherit from System.ValueType (which, in turn, inherits from System.Object).

事实并非如此。所有结构(以及内置值类型,如System.Int32,System.Single等)总是从System.ValueType(它继承自System.Object)隐式继承。

However, you can't make a struct that inherits from anything else.

但是,您不能创建从其他任何东西继承的结构。

This is clearly spelled out in the C# language spec, 4.1.1:

这在C#语言规范4.1.1中有明确规定:

4.1.1 The System.ValueType type

4.1.1 System.ValueType类型

All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object. It is not possible for any type to derive from a value type, and value types are thus implicitly sealed (§10.1.1.2).

所有值类型都隐式继承自System.ValueType类,而System.ValueType又继承自类对象。任何类型都不可能从值类型派生,因此值类型被隐式密封(第10.1.1.2节)。

Then, later (4.1.3) struct is explicitly defined to be a value type:

然后,稍后(4.1.3)struct被明确定义为值类型:

4.1.3 Struct types

4.1.3结构类型

A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types.

结构类型是一种值类型,可以声明常量,字段,方法,属性,索引器,运算符,实例构造函数,静态构造函数和嵌套类型。

#2


1  

Int32 implements IFormattable, which defines the ToString method

Int32实现了IFormattable,它定义了ToString方法

#3


1  

Every definition of a type derived from ValueType actually defines two distinct kinds of things in the runtime: a type of heap object (which derives from ValueType and in turn from Object, and which contains embedded type information), and a type of storage location (which doesn't contain any embedded type information, but instead requires that code which uses it must have some other means of knowing what it is). An instance of the heap object type contains a field of the storage-location type, and code which attempts to access this will access that field. If a value type is cast implicitly or explicitly to a storage location of reference type, the system will create a new heap object with the appropriate type and copy all public and private fields of the value-type to corresponding fields within the heap object. If a heap object is cast to a value-type storage location, all public and private fields from the heap object will be copied to the value-type storage location.

从ValueType派生的类型的每个定义实际上在运行时定义了两种不同的东西:一种堆对象(它从ValueType派生而来自Object,并且包含嵌入的类型信息),以及一种存储位置(它不包含任何嵌入式类型信息,而是要求使用它的代码必须具有一些其他方法来了解它是什么。堆对象类型的实例包含存储位置类型的字段,尝试访问该字段的代码将访问该字段。如果将值类型隐式或显式地转换为引用类型的存储位置,系统将创建具有适当类型的新堆对象,并将value-type的所有公共和私有字段复制到堆对象中的相应字段。如果将堆对象强制转换为值类型存储位置,则堆对象中的所有公共和私有字段都将复制到值类型存储位置。

If an attempt is made to use any Object or interface method on a heap object of a value type, the method will be called just like any other heap-object method. If the attempt is made on a value-type storage location, for any method other than GetType, the compiler will generate a special "constrained" opcode which informs the runtime of the type of the storage location and instructs the Runtime to call the address of the method appropriate to that type. Since the compiler will have the storage location type available, and the run-time can use that to find the appropriate method, the appropriate method can be invoked directly on the storage location, without having to create a new heap object first. GetType is the one notable exception; since it works by examining the type information embedded within an object, it can only work on things that have embedded type information. Consequently, its argument will be converted to the heap-object form before the call; GetType will then be able to examine the embedded type information of that heap object.

如果尝试在值类型的堆对象上使用任何Object或接口方法,则将像调用任何其他堆对象方法一样调用该方法。如果尝试在值类型存储位置上进行,对于除GetType之外的任何方法,编译器将生成一个特殊的“约束”操作码,该操作码通知运行时存储位置的类型,并指示运行时调用地址适合该类型的方法。由于编译器将具有可用的存储位置类型,并且运行时可以使用它来查找适当的方法,因此可以直接在存储位置调用适当的方法,而无需首先创建新的堆对象。 GetType是一个值得注意的例外;因为它通过检查嵌入在对象中的类型信息来工作,所以它只能处理具有嵌入类型信息的事物。因此,它的参数将在调用之前转换为堆对象形式;然后,GetType将能够检查该堆对象的嵌入类型信息。

#1


30  

If a struct cannot inherit some class or struct,

如果结构不能继承某个类或结构,

This isn't true. All structs (and the built-in value types, like System.Int32, System.Single, etc) always implicitly inherit from System.ValueType (which, in turn, inherits from System.Object).

事实并非如此。所有结构(以及内置值类型,如System.Int32,System.Single等)总是从System.ValueType(它继承自System.Object)隐式继承。

However, you can't make a struct that inherits from anything else.

但是,您不能创建从其他任何东西继承的结构。

This is clearly spelled out in the C# language spec, 4.1.1:

这在C#语言规范4.1.1中有明确规定:

4.1.1 The System.ValueType type

4.1.1 System.ValueType类型

All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object. It is not possible for any type to derive from a value type, and value types are thus implicitly sealed (§10.1.1.2).

所有值类型都隐式继承自System.ValueType类,而System.ValueType又继承自类对象。任何类型都不可能从值类型派生,因此值类型被隐式密封(第10.1.1.2节)。

Then, later (4.1.3) struct is explicitly defined to be a value type:

然后,稍后(4.1.3)struct被明确定义为值类型:

4.1.3 Struct types

4.1.3结构类型

A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types.

结构类型是一种值类型,可以声明常量,字段,方法,属性,索引器,运算符,实例构造函数,静态构造函数和嵌套类型。

#2


1  

Int32 implements IFormattable, which defines the ToString method

Int32实现了IFormattable,它定义了ToString方法

#3


1  

Every definition of a type derived from ValueType actually defines two distinct kinds of things in the runtime: a type of heap object (which derives from ValueType and in turn from Object, and which contains embedded type information), and a type of storage location (which doesn't contain any embedded type information, but instead requires that code which uses it must have some other means of knowing what it is). An instance of the heap object type contains a field of the storage-location type, and code which attempts to access this will access that field. If a value type is cast implicitly or explicitly to a storage location of reference type, the system will create a new heap object with the appropriate type and copy all public and private fields of the value-type to corresponding fields within the heap object. If a heap object is cast to a value-type storage location, all public and private fields from the heap object will be copied to the value-type storage location.

从ValueType派生的类型的每个定义实际上在运行时定义了两种不同的东西:一种堆对象(它从ValueType派生而来自Object,并且包含嵌入的类型信息),以及一种存储位置(它不包含任何嵌入式类型信息,而是要求使用它的代码必须具有一些其他方法来了解它是什么。堆对象类型的实例包含存储位置类型的字段,尝试访问该字段的代码将访问该字段。如果将值类型隐式或显式地转换为引用类型的存储位置,系统将创建具有适当类型的新堆对象,并将value-type的所有公共和私有字段复制到堆对象中的相应字段。如果将堆对象强制转换为值类型存储位置,则堆对象中的所有公共和私有字段都将复制到值类型存储位置。

If an attempt is made to use any Object or interface method on a heap object of a value type, the method will be called just like any other heap-object method. If the attempt is made on a value-type storage location, for any method other than GetType, the compiler will generate a special "constrained" opcode which informs the runtime of the type of the storage location and instructs the Runtime to call the address of the method appropriate to that type. Since the compiler will have the storage location type available, and the run-time can use that to find the appropriate method, the appropriate method can be invoked directly on the storage location, without having to create a new heap object first. GetType is the one notable exception; since it works by examining the type information embedded within an object, it can only work on things that have embedded type information. Consequently, its argument will be converted to the heap-object form before the call; GetType will then be able to examine the embedded type information of that heap object.

如果尝试在值类型的堆对象上使用任何Object或接口方法,则将像调用任何其他堆对象方法一样调用该方法。如果尝试在值类型存储位置上进行,对于除GetType之外的任何方法,编译器将生成一个特殊的“约束”操作码,该操作码通知运行时存储位置的类型,并指示运行时调用地址适合该类型的方法。由于编译器将具有可用的存储位置类型,并且运行时可以使用它来查找适当的方法,因此可以直接在存储位置调用适当的方法,而无需首先创建新的堆对象。 GetType是一个值得注意的例外;因为它通过检查嵌入在对象中的类型信息来工作,所以它只能处理具有嵌入类型信息的事物。因此,它的参数将在调用之前转换为堆对象形式;然后,GetType将能够检查该堆对象的嵌入类型信息。