原文 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)的更多相关文章

  1. C# 中的结构类型(struct type)

    ylbtech- .NET-Basic:C# 中的结构类型(struct type) C# 中的结构类型(struct type) 1.A,相关概念返回顶部   像类一样,结构(struct)是能够包 ...

  2. Go语言中的结构体 (struct)

    Golang官方称Go语言的语法相对Java语言而言要简洁很多,但是简洁背后也灵活了很多,所以很多看似很简单的代码上的细节稍不注意就会产生坑.本文主要对struct结构体的相关的语法进行总结和说明. ...

  3. C语言小结之结构类型

    C语言小结之结构类型 @刁钻的游戏 (1)枚举型类型enum COLOR {BLACK,RED,BLUE};//声明一种新的数据类型,其值分别为0,1,2但是用BLACK/RED/BLUE代表也可以这 ...

  4. C++中的结构体的认识

    C++中的结构体的认识 1. typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间. 实例像:typedef ...

  5. C结构体struct用法小结

    结构体和int,float等类型一样是一种常用的类型,它是由各种基本数据类型构成,通常包含有struct关键字,结构体名,结构体成员,结构体变量. 一.结构体定义 通常有3种定义方式,以例子方式表示: ...

  6. C语言入门-结构类型

    一.声明结构类型 #include <stdio.h> int main(int argc, char const *argv[]) { // 声明结构类型 struct date { i ...

  7. 内核中用于数据接收的结构体struct msghdr(转)

    内核中用于数据接收的结构体struct msghdr(转) 我们从一个实际的数据包发送的例子入手,来看看其发送的具体流程,以及过程中涉及到的相关数据结构.在我们的虚拟机上发送icmp回显请求包,pin ...

  8. C#中将结构类型数据存储到二进制文件中方法

    以往在vb6,vc6中都有现成的方法将结构类型数据写入和读取到二进制文件中,但是在c#中却没有现成的方法来实现,因此我查阅了一些资料,借鉴了网上一些同学的做法,自己写了个类似的例子来读写结构类型数据到 ...

  9. c#中结构体(struct)和类(class)的区别

    一.类与结构的示例比较: 结构示例: public struct Person { string Name; int height; int weight public bool overWeight ...

随机推荐

  1. java 构造函数

    1.public className(){}. 2.名称与类名相同,无返回值,无返回类型,void也不行.(就是上边的形式,除了可以有参数). 3.有0个或多个参数. 4.每个类都至少有一个const ...

  2. 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  3. failed to lazily initialize a collection of role

    可能修复了一个重大的偶尔发生的几乎难以察觉的并且到现在我也没能理解的bug...有时(经常)调用updateNotNullfield方法(原理是从数据库中get一个对象,然后把原对象中非空的值赋予它, ...

  4. Python 开源异步并发框架的未来

    http://segmentfault.com/a/1190000000471602 开源 Python 是开源的,介绍的这几个框架 Twisted.Tornado.Gevent 和 tulip 也都 ...

  5. 服务器程序源代码分析之三:gunicorn

    服务器程序源代码分析之三:gunicorn 时间:2014-05-09 11:33:54 类别:网站架构 访问: 641 次 gunicorn是一个python web 服务部署工具,类似flup,完 ...

  6. ajax请求返回json数据弹出下载框的解决方法

    将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...

  7. HDU4502吉哥系列故事——临时工计划

    http://acm.hdu.edu.cn/showproblem.php?pid=4502 题意 :这个是中文题,我就不再详述了. 思路 : 以前做过一个活动区间选择,结果就按着那个思路敲了,后来发 ...

  8. 一周一话题之四(JavaScript、Dom、jQuery全面复习总结<Dom篇>)

    -->目录导航 一. 初探Dom 1. Dom介绍 二. Dom基础 1. window顶级对象 2. body.document对象事件 3. 通用的HTML元素的事件 4. 冒泡事件 5. ...

  9. net.sf.json.JSONException: Object is null

    出现这个错误的原因是net.sf.json.JSONArray或JSONObject转换时,对象内包含另一个对象,而该被包含的对象为NULL,所以抛出异常. 补充: 最可恨的是,明明转换的时候已经成功 ...

  10. HeadFirst设计模式之RMI介绍

    一.使用步骤 1.generate stubs and skeletons:Run rmic on the remote implementation class 如:D:\Workspaces\My ...