自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#聲明數(shù)組的詳細解析

開發(fā) 后端
C#聲明數(shù)組在這里向你介紹了C#聲明各種常見數(shù)組,以及C#聲明數(shù)組之后的實例化,那么具體的內(nèi)容是什么呢?本文向你介紹相關(guān)內(nèi)容。

C#聲明數(shù)組的操作是什么呢?C#聲明數(shù)組的作用是什么呢?下面我們一一探討,C#數(shù)組從零開始建立索引,即數(shù)組索引從零開始。C#中數(shù)組的工作方式與在大多數(shù)其他流行語言中的工作方式類似。但還有一些差異應(yīng)引起注意。

C#聲明數(shù)組時,方括號([])必須跟在類型后面,而不是標(biāo)識符后面。在C#中,將方括號放在標(biāo)識符后是不合法的語法。

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

另一細節(jié)是,數(shù)組的大小不是其類型的一部分,而在 C 語言中它卻是數(shù)組類型的一部分。這使您可以聲明一個數(shù)組并向它分配 int 對象的任意數(shù)組,而不管數(shù)組長度如何。

  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#聲明數(shù)組細節(jié)討論:

C# 支持一維數(shù)組、多維數(shù)組(矩形數(shù)組)和數(shù)組的數(shù)組(交錯的數(shù)組)。下面的示例展示如何聲明不同類型的數(shù)組:

C#聲明數(shù)組之一維數(shù)組:

  1. int[] numbers; 

C#聲明數(shù)組之多維數(shù)組:

  1. string[,] names; 

C#聲明數(shù)組之?dāng)?shù)組的數(shù)組(交錯的):

  1. byte[][] scores; 

C#聲明數(shù)組之(如上所示)并不實際創(chuàng)建它們。在 C# 中,數(shù)組是對象(本教程稍后討論),必須進行實例化。下面的示例展示如何創(chuàng)建數(shù)組:

C#聲明數(shù)組之實例化一維數(shù)組:

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

C#聲明數(shù)組之實例化多維數(shù)組:

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

C#聲明數(shù)組之實例化數(shù)組的數(shù)組(交錯的):

  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#聲明數(shù)組之實例化三維的矩形數(shù)組:

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

甚至可以將矩形數(shù)組和交錯數(shù)組混合使用。例如,下面的代碼聲明了類型為 int 的二維數(shù)組的三維數(shù)組的一維數(shù)組。

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

C#聲明數(shù)組之實例化示例

  1. //下面是一個完整的 C# 程序,它聲明并實例化上面所討論的數(shù)組。  
  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#聲明數(shù)組之實例化示例輸出

  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#聲明數(shù)組及實例化相關(guān)的內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#聲明數(shù)組及相關(guān)內(nèi)容有所幫助。

【編輯推薦】

  1. C#數(shù)組復(fù)制方法詳解
  2. C#判斷字符串應(yīng)用詳細解析
  3. C#格式化字符串學(xué)習(xí)總結(jié)
  4. C#動態(tài)創(chuàng)建數(shù)組實現(xiàn)實例解析
  5. C#動態(tài)創(chuàng)建數(shù)組詳細實現(xiàn)過程解析
責(zé)任編輯:仲衡 來源: CSDN
相關(guān)推薦

2009-09-02 16:20:22

C#動態(tài)創(chuàng)建數(shù)組

2009-09-03 17:57:06

C#聲明事件

2009-09-18 10:00:17

C#數(shù)組操作

2009-09-02 16:30:20

C#定義數(shù)組

2009-09-27 11:14:09

C#數(shù)組

2009-09-01 18:32:32

C#動態(tài)數(shù)組

2009-08-31 14:46:15

C# string b

2009-09-02 15:53:27

C#判斷字符串應(yīng)用

2009-08-28 11:09:35

C#數(shù)組初始化

2009-09-02 14:18:08

C#聲明COM接口

2009-09-02 16:14:21

C#動態(tài)創(chuàng)建數(shù)組

2009-09-01 18:05:17

C#類型聲明

2009-09-02 10:58:02

C#動態(tài)數(shù)組

2009-08-12 15:34:40

C# DBNull

2009-08-27 17:14:36

C# Socket

2009-08-10 16:30:56

C# BitmapDa

2009-09-17 17:13:54

C#數(shù)組

2009-07-31 15:10:21

C#函數(shù)指針數(shù)組C#數(shù)組

2009-08-20 10:34:46

C#中聲明API函數(shù)

2009-09-04 09:34:03

Java和C#頂層聲明
點贊
收藏

51CTO技術(shù)棧公眾號