温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#语言知识点整理 - 结构

发布时间:2020-07-19 06:12:41 来源:网络 阅读:497 作者:勇闯天涯X 栏目:编程语言

一、 结构与类的区别:

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

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

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

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

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

5. 结构可以实现接口。

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

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

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: }

 

三、 参考链接:

C#封装c++结构体与互调用:

http://developer.51cto.com/art/200908/143782.htm

解决C#结构体数组间的转化

http://developer.51cto.com/art/200908/143779.htm

C#结构体的序列化与反序列化

http://developer.51cto.com/art/200908/144015.htm

StructLayoutAttribute Class

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx

How to: Create a C/C++ Union Using Attributes (C# Programming Guide)

http://msdn.microsoft.com/en-us/library/acxa5b99%28v=vs.80%29.aspx

附件:http://down.51cto.com/data/2362507
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI