Creates a new array with just the specified elements.
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Redwerb.BizArk.Core.ArrayExt
{
/// <summary>
/// Provides extension methods for string arrays.
/// </summary>
public static class ArrayExt
{
#region Shrink
/// <summary>
/// Creates a new array with just the specified elements.
/// </summary>
/// <param name="arr"></param>
/// <param name="startIndex"></param>
/// <param name="endIndex"></param>
/// <returns></returns>
public static Array Shrink(this Array arr, int startIndex, int endIndex)
{
if (arr == null) return null;
if (startIndex >= arr.Length) return Array.CreateInstance(arr.GetType().GetElementType(), 0);
if (endIndex < startIndex) return Array.CreateInstance(arr.GetType().GetElementType(), 0);
if (startIndex < 0) startIndex = 0;
int length = (endIndex - startIndex) + 1;
Array retArr = Array.CreateInstance(arr.GetType().GetElementType(), length);
for (int i = startIndex; i <= endIndex; i++)
retArr.SetValue(arr.GetValue(i), i - startIndex);
return retArr;
}
/// <summary>
/// Creates a new array with just the specified elements.
/// </summary>
/// <param name="arr"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static string[] Shrink(this string[] arr, int startIndex)
{
return Shrink((Array)arr, startIndex, arr.Length - 1) as string[];
}
/// <summary>
/// Creates a new array with just the specified elements.
/// </summary>
/// <param name="arr"></param>
/// <param name="startIndex"></param>
/// <param name="endIndex"></param>
/// <returns></returns>
public static string[] Shrink(this string[] arr, int startIndex, int endIndex)
{
return Shrink((Array)arr, startIndex, endIndex) as string[];
}
/// <summary>
/// Creates a new array with just the specified elements.
/// </summary>
/// <param name="arr"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static int[] Shrink(this int[] arr, int startIndex)
{
return Shrink((Array)arr, startIndex, arr.Length - 1) as int[];
}
/// <summary>
/// Creates a new array with just the specified elements.
/// </summary>
/// <param name="arr"></param>
/// <param name="startIndex"></param>
/// <param name="endIndex"></param>
/// <returns></returns>
public static int[] Shrink(this int[] arr, int startIndex, int endIndex)
{
return Shrink((Array)arr, startIndex, endIndex) as int[];
}
#endregion
}
}
Related examples in the same category