struct tostring()方法重定向到抽象类ValueType [英] struct tostring() method is redirecting to abstract class ValueType
问题描述
Int32 i =新的Int32();i.ToString();
ToString方法重定向 struct Int32
和 .ToString()
具有 override
所以我想检查它覆盖的地方,所以我创建了一个 struct
The ToString method redirecting struct Int32
and .ToString()
has a override
so I want to check where it is overriding so I created a struct
struct myStruct {}
myStruct my= new myStruct();
my.ToString();
当我重定向时,我按f12键将其移至公共抽象类ValueType
但是我们不能将类继承到结构中
when I redirecting I mean press f12 it moved to public abstract class ValueType
But we can't inherit class into a struct
所以问题是我如何获得 my.ToString();
?
So the question is how I'm getting my.ToString();
?
推荐答案
所有值类型均隐式地从 System.ValueType 派生.
还值得注意的是( ValueType.ToString 方法将覆盖 Object.ToString 方法,并为值类型提供 ToString 方法的默认实现.(值类型是由C#
The ValueType.ToString method overrides the Object.ToString method and provides the default implementation of the ToString method for value types. (Value types are types defined by the struct keyword in C# 根据语言设计, 例如,如果我们查看c#源代码中的Int类型,我们看到它是一个结构,但是出于相同的原因,我们可以调用 if for instance we look at the Int type in the c# source code, we see it's a struct but we can call the methods that available on the 我们总是可以查看这些基类库的源代码.在此处查看ValueType.cs的代码 We can always look in to the source code of these Base class libraries. See the code for ValueType.cs here 这篇关于struct tostring()方法重定向到抽象类ValueType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Struct
和基本类型从 ValueType
继承.因此,正是在这种语言之内,我们才能在其上调用 ToString()
,因为 ValueType
是一个类,并且最终从 Object
继承自每种类型都在C#中继承.Struct
and primitive types inherit from ValueType
as per language design. So it is within the language that's why we can call the ToString()
on it as the ValueType
is a class and eventually inherits from Object
from which every type inherits in C#. Object
类上可用的方法.Object
class for the same reason.