当前位置:网站首页>C#入门系列(十三) -- 初识结构体
C#入门系列(十三) -- 初识结构体
2022-06-13 09:13:00 【InfoQ】
概念
定义
[public] struct 结构体类型名{
成员声明;
}
- 成员声明描述了该类型的数据成员(或称为数据元素)以及其他类型成员的集合。
- 成员的数目可以任意多个,由具体需求确定。
- 大括号是成员列表的边界符。
public 成员类型 成员名称列表;
- 例如,对于坐标点信息的表示可以建立下面的结构体类型。
struct Ponit{
public int X; //横坐标
public int Y; //纵坐标
}
补充说明
- struct是定义结构体类型的一个关键字,不代表一种数据类型。
- 只有使用struct关键字定义了具体的结构体类型之后才能定义相应的变量,因此不能使用struct关键字来作为变量的数据类型。
- 结构体类型的成员既可以是简单数据类型的,也可以是结构体类型的,即结构体的定义可以嵌套。比如:
struct Ponit{
public int X; //横坐标
public int Y; //纵坐标
}
struct Ponit_3{
public Ponit Pos_X_Y;
public int Z;
}
- C#不允许结构体内部的成员类型是本结构体。如:
struct Ponit{
public int X; //横坐标
public int Y; //纵坐标
// 这样是错误 的
public Ponit pos;
}
- C#中的结构体除了数据成员之外,它也可以有函数成员。
- 结构体与类最大的区别就是类是引用类型,而结构是值类型。结构是密封的、不能继承的。
边栏推荐
猜你喜欢

Message Oriented Middleware

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

C language: Simulated Implementation of library function strcpy
Drill down to protobuf - Introduction

消息中间件

C language: deep understanding of character functions and string functions (2)

教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5

20220606 关于矩阵的Young不等式

Neo4j - CQL使用

The Jenkins console does not output custom shell execution logs
随机推荐
202012 CCF test questions
消息中间件
HAProxy + Keepalived实现MySQL的高可用负载均衡
Class loading overview
教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5
20211108 observable, controllable, stable and measurable
JUC Unsafe
BGP Federation +community
【最全面详细解释】背包问题详解
20211104 why are the traces of similar matrices the same
Solov2 source code analysis
LeetCode 72. 编辑距离
QML compilation specification
批量读取文件夹下的全部语音文件
20211005 Hermite矩阵及几个性质
C language: timer principle
C/s model and P2P model
What are the bank financial products? How long is the liquidation period?
Use typescript to complete simple snake eating function
批量讀取文件夾下的全部語音文件