C# 中的结构类型(struct)

简介:

原文 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;  
            }  
        }  
    }  
复制代码

默认构造器不需要也不能自己定义,默认构造器会把所有的自动初始化为 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(12, 30);  
            }  
        }  
      
        struct Time  
        {  
            private int hours, minutes, seconds;  
      
            public Time(int hh, int mm)  
            {  
                hours = hh;  
                minutes = mm;  
                seconds = 0;  
            }  
        }  
      
    }  
复制代码

 

字段(field)值如下

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

 

复制代码
    struct Time  
    {  
        private int hours = 0;  // 报错 'Time.hours': cannot have   
                                // instance field initializers in structs  
      
        private int minutes, seconds;  
      
        public Time(int hh, int mm)  
        {  
            hours = hh;  
            minutes = mm;  
            seconds = 0;  
        }  
    }  
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。



    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5947914.html,如需转载请自行联系原作者


相关文章
|
1月前
|
机器学习/深度学习 C语言
自己建立结构体类型
自己建立结构体类型
9 0
|
7月前
|
存储 编译器 C++
32.【C/C++ 结构体全类型 (详解)】(一)
32.【C/C++ 结构体全类型 (详解)】
42 0
|
7月前
|
存储 C++
32.【C/C++ 结构体全类型 (详解)】(二)
32.【C/C++ 结构体全类型 (详解)】
43 0
|
1月前
|
C++
22结构体类型
22结构体类型
12 0
|
4月前
结构体类型操作
自定义一个结构体类型的变量,其成员包括学号、姓名、年龄、性别,并将其类型声明为student,然后用该类型定义一个stu1的变量,进行赋值操作,并输出其值。
28 1
|
4月前
|
存储 C++
[C++] 结构体Struct类型和变量定义
[C++] 结构体Struct类型和变量定义
36 0
|
4月前
|
C语言 C++
[C++&C] Struct 和Typedef Struct的区别
[C++&C] Struct 和Typedef Struct的区别
58 0
|
6月前
|
存储 C语言
浅谈结构体类型
浅谈结构体类型
|
8月前
|
C语言
struct结构体初识
struct结构体初识
34 0
|
9月前
|
存储
结构体基础
结构体基础

热门文章

最新文章