Base class of struct construct in C#

安稳与你 提交于 2020-01-03 11:32:54

问题


http://msdn.microsoft.com/en-us/library/ah19swz4(v=VS.71).aspx

As per the above link….. “Structs, however, inherit from the base class Object……”

As per the below link http://msdn.microsoft.com/en-us/library/system.valuetype.aspx Struct is implemented after ValueType in the hierarchy.

“struct” is derived from which class? Or compiler treats “struct” reserve word to make any declaration using “struct” as value type? Missing the small thread in overall understanding. Thank you for your help. Smith


回答1:


The hierarchies (skipping any class subtypes) are:

  1. struct .. -> ValueType -> Object

  2. class .. -> Object

Demo:

struct S {}
class C {}

// or see `is` as per Jeff Mercado's comment
typeof(ValueType).IsAssignableFrom(typeof(S)); // True
typeof(object).IsAssignableFrom(typeof(S));    // True

typeof(ValueType).IsAssignableFrom(typeof(C)); // False
typeof(object).IsAssignableFrom(typeof(C));    // True


来源:https://stackoverflow.com/questions/11461789/base-class-of-struct-construct-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!