C#声明数组的详细解析

开发 后端
C#声明数组在这里向你介绍了C#声明各种常见数组,以及C#声明数组之后的实例化,那么具体的内容是什么呢?本文向你介绍相关内容。

C#声明数组的操作是什么呢?C#声明数组的作用是什么呢?下面我们一一探讨,C#数组从零开始建立索引,即数组索引从零开始。C#中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。

C#声明数组时,方括号([])必须跟在类型后面,而不是标识符后面。在C#中,将方括号放在标识符后是不合法的语法。

  1. int[] table; // not int table[];   

另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。

  1. int[] numbers; // declare numbers as an int array of any size  
  2.  
  3. numbers = new int[10];  // numbers is a 10-element array  
  4.  
  5. numbers = new int[20];  // now it's a 20-element array  

C#声明数组细节讨论:

C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组:

C#声明数组之一维数组:

  1. int[] numbers; 

C#声明数组之多维数组:

  1. string[,] names; 

C#声明数组之数组的数组(交错的):

  1. byte[][] scores; 

C#声明数组之(如上所示)并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:

C#声明数组之实例化一维数组:

  1. int[] numbers = new int[5]; 

C#声明数组之实例化多维数组:

  1. string[,] names = new string[5,4]; 

C#声明数组之实例化数组的数组(交错的):

  1. byte[][] scores = new byte[5][];  
  2.  
  3. for (int x = 0; x < scores.Length; x++)   
  4.  
  5. {  
  6.  
  7. scores[x] = new byte[4];  
  8.  
  9. }  

C#声明数组之实例化三维的矩形数组:

  1. int[,,] buttons = new int[4,5,3]; 

甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组。

  1. int[][,,][,] numbers; 

C#声明数组之实例化示例

  1. //下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。  
  2.  
  3. // arrays.cs  
  4.  
  5. using System;  
  6.  
  7. class DeclareArraysSample  
  8. {  
  9. public static void Main()  
  10. {  
  11. // Single-dimensional array  
  12. int[] numbers = new int[5];  
  13. // Multidimensional array  
  14. string[,] names = new string[5,4];  
  15. // Array-of-arrays (jagged array)  
  16. byte[][] scores = new byte[5][];  
  17. // Create the jagged array  
  18.  
  19. for (int i = 0; i < scores.Length; i++)  
  20. {  
  21. scores[i] = new byte[i+3];  
  22. }  
  23. // Print length of each row  
  24.  
  25. for (int i = 0; i < scores.Length; i++)  
  26.  
  27. {  
  28. Console.WriteLine(  
  29. "Length of row {0} is {1}", i, scores[i].Length);  
  30. }  
  31. }  
  32. }  

C#声明数组之实例化示例输出

  1. Length of row 0 is 3  
  2.  
  3. Length of row 1 is 4  
  4.  
  5. Length of row 2 is 5  
  6.  
  7. Length of row 3 is 6  
  8.  
  9. Length of row 4 is 7  

C#声明数组及实例化相关的内容就向你介绍到这里,希望对你了解和学习C#声明数组及相关内容有所帮助。

【编辑推荐】

  1. C#数组复制方法详解
  2. C#判断字符串应用详细解析
  3. C#格式化字符串学习总结
  4. C#动态创建数组实现实例解析
  5. C#动态创建数组详细实现过程解析
责任编辑:仲衡 来源: CSDN
相关推荐

2009-09-02 16:20:22

C#动态创建数组

2009-09-03 17:57:06

C#声明事件

2009-09-18 10:00:17

C#数组操作

2009-09-02 16:30:20

C#定义数组

2009-09-27 11:14:09

C#数组

2009-08-31 14:46:15

C# string b

2009-09-01 18:32:32

C#动态数组

2009-09-02 15:53:27

C#判断字符串应用

2009-08-28 11:09:35

C#数组初始化

2009-09-02 14:18:08

C#声明COM接口

2009-09-01 18:05:17

C#类型声明

2009-09-02 16:14:21

C#动态创建数组

2009-09-02 10:58:02

C#动态数组

2009-08-10 16:30:56

C# BitmapDa

2009-08-27 17:14:36

C# Socket

2009-08-20 10:34:46

C#中声明API函数

2009-08-12 15:34:40

C# DBNull

2009-07-31 15:10:21

C#函数指针数组C#数组

2009-09-04 09:34:03

Java和C#顶层声明

2009-09-17 17:13:54

C#数组
点赞
收藏

51CTO技术栈公众号