C#中的结构 [英] Structure in c#

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

问题描述

为什么不可能从其他用户定义的类继承结构.

Why it is not possible inherit struct from other user defined class.

推荐答案

struct s是CLR的值类型,并按值传递,并且您不能继承值类型(例如int,long等).
阅读这些内容: http://msdn.microsoft.com/zh-CN /library/ah19swz4%28v=vs.71%29.aspx [> http://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance [^ ]
structs are value types to the CLR and are passed by value, and you cannot inherit value types (like int, long etc.)
Read these : http://msdn.microsoft.com/en-us/library/ah19swz4%28v=vs.71%29.aspx[^]

http://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance[^]


嘿,你又回来了:)希望这种解释可以帮助您理解为什么struct无法继承.

在C#语言规范4.1.1中清楚地阐明了这一点:


Hey you back with another :) hope this explanation helps you to understand why struct cant inherits.

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


报价:

4.1.1 System.ValueType类型

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

然后,稍后(4.1.3)的struct被显式定义为值类型:


4.1.3结构类型

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

4.1.1 The System.ValueType type

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).

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


4.1.3 Struct types

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



更多说明:



more explanation:

报价:

想象一下结构支持的继承.然后声明:
BaseStruct a;
InheritedStruct b; //继承自BaseStruct,添加的字段等.

a = b; //??在分配作业期间扩大尺寸?

表示结构变量没有固定的大小,这就是为什么我们有引用类型.

更好的是考虑一下:
BaseStruct [] baseArray = new BaseStruct [1000];

baseArray [500] = new InheritedStruct(); //??变形/调整数组大小?

Imagine structs supported inheritance. Then declaring:
BaseStruct a;
InheritedStruct b; //inherits from BaseStruct, added fields, etc.

a = b; //?? expand size during assignment?

would mean struct variables don''t have fixed size, and that is why we have reference types.

Even better, consider this:
BaseStruct[] baseArray = new BaseStruct[1000];

baseArray[500] = new InheritedStruct(); //?? morph/resize the array?


我很惊讶地看到人们试图合理地解释"它的一些解决方案.对我来说,这是绝对清楚的,任何解释都是错误的.这无非是.NET作者的决定.对值类型(尤其是结构)的继承绝对没有错.在某些系统中,它是通常的功能.而且,有些系统继承了原始类型,这确实带来了很大的好处.

我个人认为这是.NET的谬论之一.虽然我在.NET中发现的谬论很少,但这是一个基本的谬论,导致许多困难.如果赞成这个决定或反对意见,可能有很多论点,例如我的.但是,没有纯粹的技术性"解释,并且这样的解释不存在-这是纯粹的人为决定.

—SA
I was surprised to see some solutions where people try to rationally "explain" it. To me, this is absolutely clear that any explanations are wrong. This is nothing more than a decision of the authors of .NET. There is absolutely nothing wrong with having inheritance for value types, in particular, structure. There are systems where it is a usual feature. Moreover, there are systems where primitive types are inherited, which really great benefits.

My own opinion is that this is one of the .NET fallacies. I found very few fallacies in .NET though, but this is a fundamental one, causing number of difficulties. There can be many arguments if favor of this decision or the opposite opinion, like mine. But there are no pure "technical" explanations, and such explanations cannot exist — this is a pure human decision.

—SA


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

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