代码改变世界

C#中的struct

2015-06-03 21:36  F_Code  阅读(369)  评论(0编辑  收藏  举报

平常在编程中很少使用到struct,今天在写一个算法题的过程中需要用到,于是到网上查了一下,发现struct里面还是有很多东西我是不知道的,接下来就将我查的资料记录下来与大家一起分享。

首先结构是值类型。

结构是使用 struct 关键字定义的,结构如下:

struct 结构名
{
}

结构概述

结构具有以下特点:

  • 结构是值类型,而类是引用类型。 (结构不能包含显式的无参数构造函数)

  • 与类不同,结构的实例化可以不使用 new 运算符。

  • 结构可以声明构造函数,但它们必须带参数。

  • 一个结构不能从另一个结构或类继承,而且不能作为一个类的基。所有结构都直接继承自 System.ValueType,后者继承自System.Object

  • 结构可以实现接口。

  • 结构在定义变量时不能给定初始值。(另加)

  • 如果要在结构中使用构造函数则必须给所有的变量赋值(在构造函数中直接给变量赋值而不是给变量的属性赋值,因为在未赋值之前属性是没有值的所以不能直接给属性)(另加)

  • 所有的结构都隐式继承自ValueType类,不需要显示指定;。(另加)

  • 结构的继承列表中只允许有接口。(另加)

  • 结构的继承是三层的:object >> valuetype >> "结构" 。(另加)

例如:

 1  struct Employeestruct
 2     {
 3         private string name;
 4         public string Name
 5         {
 6             get { return name; }
 7             set { name = value; }
 8         }
 9 
10         private int age;
11         public int Age
12         {
13             get { return age; }
14             set { age = value; }
15         }
16 
17         private int gongzhi;
18         public int Gongzhi
19         {
20             get { return gongzhi; }
21             //set { gongzhi = value; }
22         }
23         //有参数构造函数
24         public Employeestruct(string _name, int _age, int _gongzhi)
25         {
26             //如果要在结构中使用构造函数则必须给所有的变量赋值(在构造函数中赋值)
27             this.name = _name;
28             this.age = _age;
29             this.gongzhi = _gongzhi;
30         }
31     }
32 
33     //使用结构
34 
35     class Program
36     {
37         static void Main(string[] args)
38         {
39 
40             ////实例化Employeestruct结构
41             //Employeestruct empstruct = new Employeestruct("Steven", 22, 10000);
42             //Console.WriteLine("姓名:{0}\n年龄:{1}\n工资:{2:C2}", empstruct.Name, empstruct.Age, empstruct.Gongzhi);
43             Console.Read();
44         }
45     }

使用结构

struct 类型适于表示 Point、Rectangle 和 Color 等轻量对象。尽管可以将一个点表示为类,但在某些情况下,使用结构更有效。例如,如果声明一个 1000 个 Point 对象组成的数组,为了引用每个对象,则需分配更多内存;这种情况下,使用结构可以节约资源。由于 .NET Framework 包含名为 Point 的对象,因此我们转而调用结构“CoOrds”。

1 public struct CoOrds
2 {
3     public int x, y;
4     public CoOrds(int p1, int p2)
5     {
6         x = p1;
7         y = p2;
8     }
9 }

声明结构的默认(无参数)构造函数是错误的。总是提供默认构造函数以将结构成员初始化为它们的默认值。在结构中初始化实例字段也是错误的。

如果使用 new 运算符创建结构对象,则会创建该结构对象,并调用适当的构造函数。与类不同,结构的实例化可以不使用 new 运算符。如果不使用 new,则在初始化所有字段之前,字段都保持未赋值状态且对象不可用。

对于结构,不像类那样存在继承。一个结构不能从另一个结构或类继承,而且不能作为一个类的基。但是,结构从基类 Object 继承。结构可实现接口,其方式同类完全一样。

与 C++ 不同,无法使用 struct 关键字声明类。在 C# 中,类与结构在语义上是不同的。结构是值类型,而类是引用类型。有关更多信息,请参见值类型。

除非需要引用类型语义,否则系统将较小的类作为结构处理效率会更高。

示例1:



下面的示例演示使用默认构造函数和参数化构造函数的 struct 初始化。
 1 public struct CoOrds
 2 {
 3     public int x, y;
 4     public CoOrds(int p1, int p2)
 5     {
 6         x = p1;
 7         y = p2;
 8     }
 9 }
10 // Declare and initialize struct objects.
11 class TestCoOrds
12 {
13     static void Main()
14     {
15         // Initialize:   
16         CoOrds coords1 = new CoOrds();
17         CoOrds coords2 = new CoOrds(10, 10);
18 
19         // Display results:
20         System.Console.Write("CoOrds 1: ");
21         System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
22 
23         System.Console.Write("CoOrds 2: ");
24         System.Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
25     }
26 }

输出


 

CoOrds 1: x = 0, y = 0

 

CoOrds 2: x = 10, y = 10

 

示例2:


 


下面举例说明了结构特有的一种功能。它在不使用 new 运算符的情况下创建 CoOrds 对象。如果将 struct 换成 class,程序将不会编译。

 1 public struct CoOrds
 2 {
 3     public int x, y;
 4     public CoOrds(int p1, int p2)
 5     {
 6         x = p1;
 7         y = p2;
 8     }
 9 }
10 
11 // Declare a struct object without "new."
12 class TestCoOrdsNoNew
13 {
14     static void Main()
15     {
16         // Declare an object:
17         CoOrds coords1;
18 
19         // Initialize:
20         coords1.x = 10;
21         coords1.y = 20;
22 
23         // Display results:
24         System.Console.Write("CoOrds 1: ");
25         System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
26     }
27 }

输出


CoOrds 1: x = 10, y = 20