CSharp examples for System:Array Null Element
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
using System.Text; using System.Runtime.InteropServices; using System.Reflection.Emit; using System.Reflection; using System.Linq; using System.Globalization; using System.Collections.Generic; using System.Collections; using System;/*from w ww .ja v a 2s. co m*/ public class Main{ /// <summary> /// Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="newLength"></param> /// <returns></returns> public static T[] CopyOf<T>(T[] array, int newLength) { T[] newArray = new T[newLength]; if (newArray.Length >= array.Length) { if (typeof(T) == typeof(string)) { // append with null or 0 for (int i = 0; i < array.Length; i++) { newArray[i] = array[i]; } } else { Buffer.BlockCopy(array, 0, newArray, 0, array.Length * Marshal.SizeOf<T>()); } return newArray; } // shrinking of the array if (newArray.Length < array.Length) { if (typeof(T) == typeof(string)) { for (int i = 0; i < newArray.Length; i++) { newArray[i] = array[i]; } } else { Buffer.BlockCopy(array, 0, newArray, 0, newArray.Length * Marshal.SizeOf<T>()); } return newArray; } throw new NotImplementedException(); } }