新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

C#语言知识点整理-结构-创新互联

一、结构与类的区别:

成都创新互联公司从2013年开始,是专业互联网技术服务公司,拥有项目成都网站建设、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元中站做网站,已为上家服务,为中站各地企业和个人服务,联系电话:18982081108

结构与类共享大多数相同的语法,但结构比类受到的限制更多:

1. 结构是值类型,而类是引用类型。

2. 在结构声明中,除非字段被声明为 const 或 static,否则无法初始化。

3. 结构不能声明默认构造函数(没有参数的构造函数)或析构函数。结构可以声明带参数的构造函数。

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

5. 结构可以实现接口。

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

7. 结构在赋值时进行复制。 将结构赋值给新变量时,将复制所有数据,并且对新副本所做的任何修改不会更改原始副本的数据。在使用值类型的集合(如 Dictionary)时,请务必记住这一点。

8. 结构可以为 null 的类型,因而可向其赋 null 值。

二、结构体示例:

1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: 
6: namespace CSharp.Struct
7: {
8: 
9:     public struct CoOrds //不能从另一个结构或类继承,但可实现接口
10:     {
11:         public int x, y;
12: 
13:         //结构中不能有实例字段初始值,除非被声明为 const 或 static
14:         //public int x = 0;
15:         //public int y = 0;
16: 
17:         //结构不能声明默认构造函数(没有参数的构造函数)或析构函数
18:         //public CoOrds()
19:         //{
20: 
21:         //}
22: 
23:         //结构可以声明带参数的构造函数。
24:         public CoOrds(int p1, int p2)
25:         {
26:             x = p1;
27:             y = p2;
28:         }
29:     }
30: 
31:     class Program
32:     {
33:         static void Main(string[] args)
34:         {
35:             //
36:             //与类不同,结构的实例化可以不使用 new 运算符。
37:             //
38: 
39:             // Declare an object:
40:             CoOrds coords1;
41: 
42:             // Initialize:
43:             coords1.x = 10;
44:             coords1.y = 20;
45: 
46:             // Display results:
47:             Console.Write("CoOrds 1: ");
48:             Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
49: 
50:             // Initialize:
51:             CoOrds coords2 = new CoOrds();
52:             CoOrds coords3 = new CoOrds(10, 10);
53:  
54:             // Display results:
55:             Console.Write("CoOrds 2: ");
56:             Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
57: 
58:             Console.Write("CoOrds 3: ");
59:             Console.WriteLine("x = {0}, y = {1}", coords3.x, coords3.y);
60: 
61:             //
62:             //将结构赋值给新变量时,将复制所有数据,并且对新副本所做的任何修改不会更改原始副本的数据。
63:             //
64: 
65:             //Copy
66:             Console.Write("After Copy:\n");
67:             coords2 = coords3;
68:             coords2.x = 20;
69:             // Display results:
70:             Console.Write("CoOrds 2: ");
71:             Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
72: 
73:             Console.Write("CoOrds 3: ");
74:             Console.WriteLine("x = {0}, y = {1}", coords3.x, coords3.y);
75: 
76:             // Keep the console window open in debug mode.
77:             Console.WriteLine("Press any key to exit.");
78:             Console.ReadKey();
79:
80:         }
81:     }
82: }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章题目:C#语言知识点整理-结构-创新互联
网页路径:http://www.wjwzjz.com/article/cdchpg.html
在线咨询
服务热线
服务热线:028-86922220
TOP