C# 中的结构类型(struct)

5 篇文章 0 订阅

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


        编译器始终会生成一个默认的构造器,若自己写默认构造器则会出错(默认构造器始终存在)。自己只能写非默认构造器,并且在自己写的构造器中初始化所有字段。

[csharp]  view plain  copy
  1. struct Time  
  2. {  
  3.     public Time()  
  4.     {   
  5.        // 编译时错误:Structs cannot contain explicit parameterless constructors  
  6.     }  
  7. }  
  8.   
  9. struct NewYorkTime  
  10. {  
  11.     private int hours, minutes, seconds;  
  12.   
  13.     public NewYorkTime(int hh, int mm)  
  14.     {  
  15.         hours = hh;  
  16.         minutes = mm;  
  17.     }   // 编译时错误,因为 seconds 未初始化  
  18. }  

        可以使用 ? 修饰符创建一个结构变量的可空(nullable)的版本。然后把 null 值赋给这个变量。

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace structType  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             NewYorkTime? currentTime = null;    // 结构类型也是值类型,可以声明为可空  
  13.         }  
  14.     }  
  15.   
  16.     struct NewYorkTime  
  17.     {  
  18.         private int hours, minutes, seconds;  
  19.   
  20.         public NewYorkTime(int hh, int mm)  
  21.         {  
  22.             hours = hh;  
  23.             minutes = mm;  
  24.             seconds = 0;  
  25.         }  
  26.     }  
  27. }  
        默认构造器不需要也不能自己定义,默认构造器会把所有的自动初始化为 0 。

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace structType  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Time now = new Time();  // 调用默认构造器,从而自动初始化,所有字段为 0  
  13.         }  
  14.     }  
  15.   
  16.     struct Time  
  17.     {  
  18.         private int hours, minutes, seconds;  
  19.     }  
  20. }  

        字段(field)值如下:


        下面这种方式,结构将不会被初始化,但是也不能访问。

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace structType  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Time now;  // 不进行初始化,若访问字段的值会造成编译错误  
  13.         }  
  14.     }  
  15.   
  16.     struct Time  
  17.     {  
  18.         private int hours, minutes, seconds;  
  19.     }  
  20. }  
        字段(field)值如下

        自己定义的构造器必须在构造器内把所有的字段初始化。

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace structType  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Time now = new Time(12, 30);  
  13.         }  
  14.     }  
  15.   
  16.     struct Time  
  17.     {  
  18.         private int hours, minutes, seconds;  
  19.   
  20.         public Time(int hh, int mm)  
  21.         {  
  22.             hours = hh;  
  23.             minutes = mm;  
  24.             seconds = 0;  
  25.         }  
  26.     }  
  27.   
  28. }  
        字段(field)值如下

        结构中的字段不能在声明的同时进行初始化。

[csharp]  view plain  copy
  1. struct Time  
  2. {  
  3.     private int hours = 0;  // 报错 'Time.hours': cannot have   
  4.                             // instance field initializers in structs  
  5.   
  6.     private int minutes, seconds;  
  7.   
  8.     public Time(int hh, int mm)  
  9.     {  
  10.         hours = hh;  
  11.         minutes = mm;  
  12.         seconds = 0;  
  13.     }  
  14. }  
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值