原文 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. 微软职位内部推荐-Senior NLP Scientist & Developer

    微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com )Senior Software Development Engineer/NL ...

  2. sql之表连接和group by +组函数的分析

    1.首先我们来先看一个简单的例子: 有[Sales.Orders]订单表和[Sales.Customers]顾客表,表的机构如下 业务要求:筛选  来自“按时打算”国家的用户以及所下的订单数 sele ...

  3. iOS9下修改回HTTP模式进行网络请求

    升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错 The resource could not be loaded because the App Transport Sec ...

  4. [转]popwindow用法

    [转]弹出窗口的两种实现方式 PopupWindow 和 Activity  链接:http://www.cnblogs.com/winxiang/archive/2012/11/20/2778729 ...

  5. hibernate映射文件基础

    一.利用hibernate的eclipse插件快速生成实体类与配置文件的方法 1.首先下载安装和自己的eclipse版本配套的hibernate tools,如果是Myeclipse,在/readme ...

  6. The 5th Zhejiang Provincial Collegiate Programming Contest------ProblemA:Accurately Say "CocaCola"!

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2965 题意:一群人玩过“7”的游戏,有7的数字或者7的倍数就要喊“coca ...

  7. console中应用MFC类的方法

    1.添加#include <afx.h>或者<afxwin.h> 这时会报错1>c:\program files\microsoft visual studio 8\vc ...

  8. 【leetcode】Divide Two Integers (middle)☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  9. linux pts/0的含义

    pts是所谓的伪终端或虚拟终端,具体表现就是你打开一个终端,这个终端就叫pts/0,如果你再打开一个终端,这个新的终端就叫pts /1.比如用who命令查询当前登录的用户,可以看到每个用户的TTY设备 ...

  10. linux 磁盘读写性能测试

    1. 测试读取速度 haparm -Tt /dev/xxx 1.1 获取硬盘设备名称: fdisk -l Disk /dev/xvdf: 365.0 GB, 365041287168 bytes 25 ...