C# 中的结构类型(struct)
简介
有时候,类中只包含极少的数据,因为管理堆而造成的开销显得极不合算。这种情况下,更好的做法是使用结构(struct)类型。由于 struct 是值类型,是在栈(stack)上存储的,所以能有效的减少内存管理的开销(当然前提是这个结构足够小)。
结构可以包含它自己的字段、方法和构造器。
int 实际上是 Sysytem.Int32 结构类型。

默认构造器(构造函数)
编译器始终会生成一个默认的构造器,若自己写默认构造器则会出错(默认构造器始终存在)。自己只能写非默认构造器,并且在自己写的构造器中初始化所有字段。
struct Time
{
public Time()
{
// 编译时错误:Structs cannot contain explicit parameterless constructors
}
} struct NewYorkTime
{
private int hours, minutes, seconds; public NewYorkTime(int hh, int mm)
{
hours = hh;
minutes = mm;
} // 编译时错误,因为 seconds 未初始化
}
可以使用 ? 修饰符创建一个结构变量的可空(nullable)的版本。然后把 null 值赋给这个变量。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace structType
{
class Program
{
static void Main(string[] args)
{
NewYorkTime? currentTime = null; // 结构类型也是值类型,可以声明为可空
}
} struct NewYorkTime
{
private int hours, minutes, seconds; public NewYorkTime(int hh, int mm)
{
hours = hh;
minutes = mm;
seconds = ;
}
}
}
默认构造器不需要也不能自己定义,默认构造器会把所有的自动初始化为 0 。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace structType
{
class Program
{
static void Main(string[] args)
{
Time now = new Time(); // 调用默认构造器,从而自动初始化,所有字段为 0
}
} struct Time
{
private int hours, minutes, seconds;
}
}
字段(field)值如下:

下面这种方式,结构将不会被初始化,但是也不能访问。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace structType
{
class Program
{
static void Main(string[] args)
{
Time now; // 不进行初始化,若访问字段的值会造成编译错误
}
} struct Time
{
private int hours, minutes, seconds;
}
}
字段(field)值如下

自定义构造器
自己定义的构造器必须在构造器内把所有的字段初始化。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace structType
{
class Program
{
static void Main(string[] args)
{
Time now = new Time(, );
}
} struct Time
{
private int hours, minutes, seconds; public Time(int hh, int mm)
{
hours = hh;
minutes = mm;
seconds = ;
}
} }
字段(field)值如下

结构中的字段不能在声明的同时进行初始化。
struct Time
{
private int hours = ; // 报错 'Time.hours': cannot have
// instance field initializers in structs private int minutes, seconds; public Time(int hh, int mm)
{
hours = hh;
minutes = mm;
seconds = ;
}
}
C# 中的结构类型(struct)的更多相关文章
- C# 中的结构类型(struct type)
ylbtech- .NET-Basic:C# 中的结构类型(struct type) C# 中的结构类型(struct type) 1.A,相关概念返回顶部 像类一样,结构(struct)是能够包 ...
- Go语言中的结构体 (struct)
Golang官方称Go语言的语法相对Java语言而言要简洁很多,但是简洁背后也灵活了很多,所以很多看似很简单的代码上的细节稍不注意就会产生坑.本文主要对struct结构体的相关的语法进行总结和说明. ...
- C语言小结之结构类型
C语言小结之结构类型 @刁钻的游戏 (1)枚举型类型enum COLOR {BLACK,RED,BLUE};//声明一种新的数据类型,其值分别为0,1,2但是用BLACK/RED/BLUE代表也可以这 ...
- C++中的结构体的认识
C++中的结构体的认识 1. typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间. 实例像:typedef ...
- C结构体struct用法小结
结构体和int,float等类型一样是一种常用的类型,它是由各种基本数据类型构成,通常包含有struct关键字,结构体名,结构体成员,结构体变量. 一.结构体定义 通常有3种定义方式,以例子方式表示: ...
- C语言入门-结构类型
一.声明结构类型 #include <stdio.h> int main(int argc, char const *argv[]) { // 声明结构类型 struct date { i ...
- 内核中用于数据接收的结构体struct msghdr(转)
内核中用于数据接收的结构体struct msghdr(转) 我们从一个实际的数据包发送的例子入手,来看看其发送的具体流程,以及过程中涉及到的相关数据结构.在我们的虚拟机上发送icmp回显请求包,pin ...
- C#中将结构类型数据存储到二进制文件中方法
以往在vb6,vc6中都有现成的方法将结构类型数据写入和读取到二进制文件中,但是在c#中却没有现成的方法来实现,因此我查阅了一些资料,借鉴了网上一些同学的做法,自己写了个类似的例子来读写结构类型数据到 ...
- c#中结构体(struct)和类(class)的区别
一.类与结构的示例比较: 结构示例: public struct Person { string Name; int height; int weight public bool overWeight ...
随机推荐
- 【多路复用】I/O多路复用
http://www.tuicool.com/articles/RBvqUz C#下用select方法实现socket服务端
- java实现.net中的枚举
Java 和 .net中的枚举不一样,在.net中,枚举是属于值类型的,而在java中确实引用类型的(其实就是一个特殊的类,enum默认集成java.lang.Enum类),所以在java中操作枚举类 ...
- iOS 10 的适配问题-b
随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,欢迎指出. 1.系统判断方法失效: 在你的项目中,当需要判断系统版本的话,不要使 ...
- csu 1312 榜单(模拟题)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1312 1312: 榜单 Time Limit: 1 Sec Memory Limit: 128 ...
- (转载)shell日志分析常用命令
shell日志分析常用命令总结 时间:2016-03-09 15:55:29来源:网络 导读:shell日志分析的常用命令,用于日志分析的shell脚本,统计日志中百度蜘蛛的抓取量.抓取最多的页面.抓 ...
- 3123 高精度练习之超大整数乘法 - Wikioi
题目描述 Description 给出两个正整数A和B,计算A*B的值.保证A和B的位数不超过100000位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Ou ...
- 3111: [Zjoi2013]蚂蚁寻路 - BZOJ
题目描述 Description在一个 n*m 的棋盘上,每个格子有一个权值,初始时,在某个格子的顶点处一只面朝北的蚂蚁,我们只知道它的行走路线是如何转弯,却不知道每次转弯前走了多长.蚂蚁转弯是有一定 ...
- UOJ Round #8 赴京赶考 解题报告
算法零 $n,m \le 100, q \le 10$ 的话,直接给网格中的每一个格点都建一个点,然后该怎么最短路就怎么最短路,该怎么并查集+BFS就怎么并查集+BFS. 复杂度 $O(qnm)$,可 ...
- [转载]获取当前日期和农历的js代码
原文地址: http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期时间: function GetCurrentDa ...
- linux服务器初步印象,远程连接mysql数据库,传输文件,启动/关闭tomcat命令
1.连接服务器数据库,以Navicat连接mysql为例 1.1 常规 新建连接,连接名,主机名或ip地址:127.0.0.1 端口:3306用户名:(服务器端)root密码:(服务器端)pwd 1. ...