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

问题描述

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

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

推荐答案


报价:

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?



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

09-18 06:18