原文 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. iOS CoCoa编程中视图控制器与视图类(转)

    分类: iPhone2012-05-28 11:19 837人阅读 评论(0) 收藏 举报 cocoa编程iosuinavigationcontrolleruiviewiphone iPhone编程规 ...

  2. 判断webpart类型 How can I tell what type a web part is?

    using(new SPSite("http://mysite/myweb").OpenWeb()){ //give relative path of the webpartpag ...

  3. 免费web直接打印的控件PAZU

    PAZU 是4Fang 四方为配合"四方在线"软件于2004年开发的WEB打印控件,适用于各种WEB软件项目的打印.PAZU是客户端软件,使用于IE作为客户端的所有应用,与服务器端 ...

  4. 慎用ReentrantLock

    前言: 代码简洁与性能高效无法两全其美,本文章专注于并发程序的性能,如果您追求代码简洁,本文章可能不太适合,本文章属于Java Concurrency in Practice读书笔记. 在java5中 ...

  5. 将ANGULAR与后端请求结合

    简单的结合,却是很多应用的基础.RESTFUL就此而生.瘦服务,富客户. <!DOCTYPE html> <html lang="en" ng-app=" ...

  6. What is the innovator’s solution——什么才是创新的解决方案1

    最近学习MOT(management of Technology),研读了Christensen的<创新者的窘境>和<创新者的解答>,以下简称创新者系列.总觉得需要写点儿什么. ...

  7. poj 3301 Texas Trip 三分法

    思路:三分法求解凸函数的极值,三分法介绍在这:http://hi.baidu.com/czyuan_acm/item/81b21d1910ea729c99ce33db 很容易就可以推出旋转后的坐标: ...

  8. linux usermod修改用户所在组方法

    usermod 用户名 -g 组名 -g<群组> 修改用户所属的群组. -G<群组> 修改用户所属的附加群组.

  9. MySql不同版本安装

    1.win7 64位下如何安装配置mysql-5.7.4-m14-winx64  1. mysql-5.7.4-m14-winx64.zip下载 2.解压到D:/mysql.(路径自己指定) 3.在D ...

  10. 【NOIP 2016 总结】

    距离杯赛已经很久了,然而我现在才打总结.. 我好惨的说..两场才380... DAY 1 第一题 toy 送分题,模拟的时候+一下再mod一下就好. [当时打完这题就没再看一眼了,好方的说] #inc ...