C#動態(tài)調(diào)整數(shù)組大小的方法
本文實(shí)例講述了C#動態(tài)調(diào)整數(shù)組大小的方法。分享給大家供大家參考。具體如下:
通常,我們創(chuàng)建一個數(shù)組后就不能調(diào)整其長度,但是Array類提供了一個靜態(tài)方法CreateInstance用來創(chuàng)建一個動態(tài)數(shù)組,所以我們可以通過它來動態(tài)調(diào)整數(shù)組的長度。
namespace ArrayManipulation { Class Program { static void Main (String[] args) { int[] arr = new int[]{1,2,3}; PrintArr(arr); arr = (int[])Redim(arr,5); PrintArr (arr); arr = (int[]) Redim (arr, 2); PrintArr (arr); ) public static Array Redim (Array origArray, int desiredSize) { //determine the type of element Type t = origArray.GetType().GetElementType(); //create a number of elements with a new array of expectations //new array type must match the type of the original array Array newArray = Array.CreateInstance (t, desiredSize); //copy the original elements of the array to the new array Array.Copy (origArray, 0, newArray, 0, Math.Min (origArray.Length, desiredSize)); //return new array return newArray; } //print array public static void PrintArr (int[] arr) { foreach (int x in arr) { Console.Write (x + ","); } Console.WriteLine (); } } }
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# 實(shí)現(xiàn)Scoket心跳機(jī)制的方法
這篇文章主要介紹了C# 實(shí)現(xiàn)Scoket心跳機(jī)制的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02silverlight實(shí)現(xiàn)圖片局部放大效果的方法
這篇文章主要介紹了silverlight實(shí)現(xiàn)圖片局部放大效果的方法,結(jié)合實(shí)例形式分析了silverlight針對圖片屬性的相關(guān)操作技巧,需要的朋友可以參考下2017-03-03C#驗(yàn)證身份證號碼正確性的實(shí)例代碼(收藏)
這篇文章主要介紹了C#驗(yàn)證身份證號碼正確性的實(shí)例代碼,包括18位號碼和15位號碼的校驗(yàn),需要的朋友可以參考下2017-07-07