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 ...
随机推荐
- 2016 系统设计第一期 (档案一)MVC ajax 获取json数据
我在做一张表的增删改查的时候,在编辑的时候,需要获取当前选择行对应的Id,然后并且把选择行的Id的对于的数据取出来,代码如下: 列表a标签绑定: Js代码: url: '/Users/GetUserB ...
- 选择排序O(n^2)与快速排序O(nlogn)的优越性代码体现
随机函数生成一个超大数组: [code]: #include <iostream> #include <stdio.h> #include<time.h> #inc ...
- [转载]MongoDB优化的几点原则
.查询优化 确认你的查询是否充分利用到了索引,用explain命令查看一下查询执行的情况,添加必要的索引,避免扫表操作. .搞清你的热数据大小 可能你的数据集非常大,但是这并不那么重要,重要的是你的热 ...
- What are the advantages of logistic regression over decision trees?FAQ
What are the advantages of logistic regression over decision trees?FAQ The answer to "Should I ...
- sql批量删除wordpress所有日志修订revision
wordpress日志修订是所有速度慢的罪恶之源,每次在后台发布或修改文章的时候,数据库都会产生一个revision版本的记录,几百篇日志会有几千条日志修订的记录,如果更多文章的话,那一个网页打开可能 ...
- 【图说】Eclipse与Unity 3D协同工作
原地址:http://blog.csdn.net/h570768995/article/details/9355313 Eclipse开发过程中总会碰到很多的难题,如何利用好工具帮助我们更快捷的开发也 ...
- python中unicode、utf8、gbk等编码问题
转自:http://luchanghong.com/python/2012/07/06/python-encoding-with-unicode-and-gbk-and-utf8.html 概要:编码 ...
- Android进程守护
http://blog.csdn.net/t12x3456/article/details/8982198 http://blog.csdn.net/ljx19900116/article/detai ...
- eclipse查看jar包中class的中文注释乱码问题的解决
1,问题来源是在eclipse中直接查看springside的class(由eclipse自动反编译)里面注释的乱码问题: Preferences-General-Workspace-Text fil ...
- c#调用命令行遇到带空格的路径
想用 c#调用如下的DOS命令: C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe /f C:\Program File ...