当前位置:网站首页>C structure: definition and example
C structure: definition and example
2022-06-26 13:02:00 【Dusk and starry sky】
C# Structure : from C/C++ A classic that has migrated from the times . Structs are similar to classes , There are also many differences . Structure pairs as C# There is a lot of value for developers . Generally, no structure is needed , But there are aspects where structs do better than classes
What is the structure ?
The structure is class The little sister of the object . See you for the first time , There is little difference between them . They all contain collections of different types of fields , as follows
Copy code
1 public struct Flower
2 {
3 public string name;
4 protected int beautyRating;
5 private bool isVascular;
6 }
Copy code
Try the structure . As a simple data structure , You can think of a structure as a field package . Maybe you will use the structure as the data transmission object , Or maybe you want to run some logic in other places based on field values .
You can also encapsulate behaviors with structures , as follows :
Copy code
1 public struct FlowerWithBehavior
2 {
3 string name;
4 int beautyRating;
5 bool isVascular;
6
7 public void makeBeautiful()
8 {
9 beautyRating = 10;
10 }
11 }
Copy code
up to now , The structure looks like a class , Just different keywords . That's all , But structures differ from classes in some important ways .
What makes the structure different ?
The most important difference is : A structure is a value type , Class is a reference type . Classes are similar to structs , as follows :
Copy code
1 public struct FlowerStruct
2 {
3 string name;
4 int beautyRating;
5 bool isVascular;
6
7 public FlowerStruct(string name, int beautyRating, bool isVascular)
8 {
9 this.name = name;
10 this.beautyRating = beautyRating;
11 this.isVascular = isVascular;
12 }
13 }
14
15 public class FlowerClass
16 {
17 public string name;
18 public int beautyRating;
19 public bool isVascular;
20
21 public FlowerClass(string name, int beautyRating, bool isVascular)
22 {
23 this.name = name;
24 this.beautyRating = beautyRating;
25 this.isVascular = isVascular;
26 }
27 }
Copy code
Although it looks similar , But classes and structs are different :
Copy code
1 public void Test_Struct_Vs_Class_Equality()
2 {
3 FlowerStruct struct1 = new FlowerStruct(“Daisy”, 3, true);
4 FlowerStruct struct2 = new FlowerStruct(“Daisy”, 3, true);
5
6 FlowerClass class1 = new FlowerClass(“Daisy”, 3, true);
7 FlowerClass class2 = new FlowerClass(“Daisy”, 3, true);
8
9 Assert.Equal(struct1, struct2);
10 Assert.NotEqual(class1, class2);
11 }
Insert picture description here
边栏推荐
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
- Redis learning - 03 transaction
- [geek challenge 2019] rce me 1
- 【网络是怎么连接的】第一章:浏览器生成消息
- Typescript learning (I) type
- opencv高速下载
- 关于NaN的一些总结
- Redis learning - 05 node JS client operation redis and pipeline pipeline
- Redis learning - 04 persistence
猜你喜欢

File remote synchronization and backup artifact Rsync

Stream learning record
![[geek challenge 2019] rce me 1](/img/66/e135f7e5a7cbdeb5b697f3939a3402.png)
[geek challenge 2019] rce me 1
微信小程序测试点总结

倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络

倍福PLC通过MC_ReadParameter读取NC轴的配置参数

processing 随机生成线动画

Splunk iowait 报警的解决

Implementing mixins scheme in applet

国标GB28181协议EasyGBS级联宇视平台,保活消息出现403该如何处理?
随机推荐
深入解析 MySQL binlog
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
el-form-item 包含两个input, 校验这两个input
. Net Maui performance improvement
This function has none of deterministic, no SQL solution
Redis learning - 05 node JS client operation redis and pipeline pipeline
Nodejs get get/post request parameters
倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
倍福NC轴状态转移图解析
KITTI Tracking dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
KVM video card transparent transmission -- the road of building a dream
Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
Learning Processing Zoog
ES6模块
Several methods added to the ES6 array (foreach, filter, some, every. Includes, reduce)
processing 随机生成线动画
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
微信小程序测试点总结
Tiger DAO VC产品正式上线,Seektiger生态的有力补充