结构与类 [英] Struct Vs Classes

查看:70
本文介绍了结构与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大师,

在C#中为什么struct不能从另一个struct继承,就像一个类

可以从另一个类继承?另一个问题是所有结构由

默认是从System.Object类继承的,当它不允许从一个类派生一个结构时,它是如何实现的?和System.Object是.NET Framework提供的

引用类型,那么为什么结构是值

类型?因为struct是从System.Object派生的,所以它们应该是

也是一个引用类型。有人可以回答这个问题吗?


谢谢和问候,

Ashish Sheth

Hi Gurus,
In C# why struct can''t be inherited from another struct, just like a class
can be inherited from another class? another question is all struct by
default are inherited from System.Object class, how this is possible when it
is not allowed to derive a struct from a class? and System.Object is the
reference type provided by the .NET Framework then why structs are value
types? since the struct are derived from System.Object then they should be
also a reference type. Can anybody answer this questions?

thanks and regards,
Ashish Sheth

推荐答案

Ashish,


结构是.NET中的一种特殊情况类型,被认为具有某些语义。此外,结构派生自ValueType,而不是Object

(但ValueType派生自Object)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv *@spam.guard.caspershouse.com

" Ashish Sheth" < EA ***** @ gmail.com>在消息中写道

新闻:O9 ************** @ TK2MSFTNGP09.phx.gbl ...
Ashish,

A structure is a special case type in .NET, and is considered to have
certain semantics. Also, structures are derived from ValueType, not Object
(but ValueType is derived from Object).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Ashish Sheth" <ea*****@gmail.com> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
嗨大师,可以从另一个类继承?另一个问题是
默认的所有结构都是从System.Object类继承的,当它不允许从类派生结构时这是怎么可能的?和System.Object是.NET Framework提供的
引用类型,那么为什么结构是值类型?因为struct是从System.Object派生的,所以它们也应该是引用类型。有人可以回答这个问题吗?

感谢和问候,
Ashish Sheth
Hi Gurus,
In C# why struct can''t be inherited from another struct, just like a class
can be inherited from another class? another question is all struct by
default are inherited from System.Object class, how this is possible when
it
is not allowed to derive a struct from a class? and System.Object is the
reference type provided by the .NET Framework then why structs are value
types? since the struct are derived from System.Object then they should be
also a reference type. Can anybody answer this questions?

thanks and regards,
Ashish Sheth



Ashish Sheth写道:
Ashish Sheth wrote:
嗨大师,
在C#中为什么struct不能从另一个struct继承,就像一个类
可以从另一个类继承?另一个问题是
默认的所有结构都是从System.Object类继承的,当它不允许从类派生结构时它是如何实现的?和System.Object是.NET Framework提供的
引用类型,那么为什么结构是值类型?因为struct是从System.Object派生的,所以它们也应该是引用类型。有人可以回答这个问题吗?
Hi Gurus,
In C# why struct can''t be inherited from another struct, just like a class
can be inherited from another class? another question is all struct by
default are inherited from System.Object class, how this is possible when it
is not allowed to derive a struct from a class? and System.Object is the
reference type provided by the .NET Framework then why structs are value
types? since the struct are derived from System.Object then they should be
also a reference type. Can anybody answer this questions?




你的逻辑存在缺陷。诸如int和decimal之类的数字类型也从System.Object派生出来,因此遵循你的逻辑,int应该是一个引用

类型,并且应该允许从类派生
由于结构是值类型,与类不同,它们不需要堆分配。此外,作为值类型,它们不能从中派生出来。结构

可以实现一个界面。

-

Tom Porterfield



Your logic is flawed. Numeric types such as int and decimal also derive
from System.Object, so following your logic an int should be a reference
type and should be allowed to derive from a class or be derived from.

As structs are value types, unlike classes they do not require heap
allocation. Also, as value types they cannot be derived from. Structs
can implement an interface.
--
Tom Porterfield


Daniel Jin< Da ******* @ discussion.microsoft.com>写道:
Daniel Jin <Da*******@discussions.microsoft.com> wrote:
结构是.NET中的一种特殊情况类型,被认为具有某些语义。此外,结构派生自ValueType,而不是Object
(但ValueType派生自Object)。
A structure is a special case type in .NET, and is considered to have
certain semantics. Also, structures are derived from ValueType, not Object
(but ValueType is derived from Object).



作为一个有趣的旁注,System.ValueType本身实际上不是值
类型。



as an interesting side note, System.ValueType itself is in fact not a value
type.




作为另一个有趣的旁注,值类型本身不会从System.ValueType继承
,或者。相反,他们相应的

盒装类型继承自ValueType,并且他们不会继承

任何东西。


它'那个阴暗的世界......


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



As another interesting side note, the value types themselves don''t
inherit from System.ValueType, either. Instead, their corresponding
boxed types inherit from ValueType, and they don''t inherit from
anything.

It''s a murky world out there...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于结构与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆