类和结构的区别是什么,用实例解释 [英] what is the difference between class and structure explain with examples

查看:63
本文介绍了类和结构的区别是什么,用实例解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用示例解释类和结构之间的区别是什么

what is the difference between class and structure explain with examples

推荐答案


在.NET中,有两类类型,引用类型和值类型.

结构是值类型,类是引用类型.

通常的不同之处在于,引用类型驻留在堆上,而值类型驻留在内联中,也就是说,无论它是在何处定义了变量或字段.

包含值类型的变量包含整个值类型值.对于结构,这意味着变量包含整个结构及其所有字段.

包含引用类型的变量包含指针,或对实际值所在的内存中其他位置的引用.

首先有一个好处:

-value类型始终包含一个值
-引用类型可以包含空引用,这意味着它们目前根本不引用任何内容

在内部,引用类型被实现为指针,并且知道引用类型并知道变量分配如何工作,还有其他行为模式:

-将值类型变量的内容复制到另一个变量中,然后将整个内容复制到新变量中,以使两者截然不同.换句话说,在复制后,对一个所做的更改不会影响另一个
-将引用类型变量的内容复制到另一个变量中,然后复制引用,这意味着您现在在实际数据的其他存储位置拥有两个相同的引用.换句话说,在复制之后,更改一个引用中的数据似乎也会影响另一个引用,但这只是因为您实际上只是在两个地方都查看相同的数据
声明变量或字段时,以下是两种类型的区别:

-variable:值类型驻留在堆栈上,引用类型驻留在堆栈上,作为指向实际内存所在的堆内存中某处的指针 -class/struct-field:值类型驻留在类内部,引用类型驻留在类内部,作为指向实际内存所在的堆内存中某处的指针.

每个的简短摘要:

仅限课程:
-可以支持继承
-是参考类型
-每个新实例都有内存开销

仅结构:
-无法支持继承
-是值类型
-每个新实例没有内存开销-除非已装箱"

两个类和结构:
-是通常用于包含一些具有某种逻辑关系的变量的复合数据类型吗?
-可以包含方法和事件
-可以支持接口

选中此复选框以获取完整的详细信息,例如:
StructsVsClasses [ ^ ]
Hi,
In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.

The general different is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.

A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.

A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.

This has one benefit, to begin with:

-value types always contains a value
-reference types can contain a null-reference, meaning that they don''t refer to anything at all at the moment

Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:

-copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won''t affect the other
-copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you''re really just looking at the same data both places
When you declare variables or fields, here''s how the two types differ:

-variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
-class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.

A short summary of each:

Classes Only:
-Can support inheritance
-Are reference types
-Have memory overhead per new instance

Structs Only:
-Cannot support inheritance
-Are value types
-Do not have a memory overhead per new instance - unless ''boxed''

Both Classes and Structs:
-Are compound data types typically used to contain a few variables that have some logical relationship
-Can contain methods and events
-Can support interfaces

Check this to get complete details with example:
StructsVsClasses[^]


^ ]
在代码项目或Google中搜索将为您提供更多文章供您参考.
Structs in C#[^]
Search in code project or google will give you lot more articles you can refer.


您好,

1.主要区别在于,默认情况下,结构内的所有声明都是公共的.类是Structure的后继.默认情况下,该类中的所有成员都是私有成员.
2.类是引用类型,而结构是值类型.
3.类存储在堆中,而结构使用堆栈.
4.类支持多态,而结构不
Hi,

1. The major difference is that all declarations inside a structure are by default public. Class is a successor of Structure. By default all the members inside the class are private.
2. Class is reference type whereas Structure is Value type.
3. Class stores in heap whereas Structure use stack.
4. classes support polymorphism, whereas structures don''t


这篇关于类和结构的区别是什么,用实例解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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