Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Searches for the specified object and returns the index of the first occurrence within the entire one-dimension

//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     {         /// <summary>         /// Searches for the specified object and returns the index of the first occurrence         /// within the entire one-dimensional System.Array.         /// </summary>         /// <param name="arr">The one-dimensional System.Array to search.</param>         /// <param name="val">The object to locate in array.</param>         /// <returns>         /// The index of the first occurrence of value within the entire array, if found;         /// otherwise, the lower bound of the array minus 1.         /// </returns>         /// <exception cref="System.ArgumentNullException">arr is null</exception>         /// <exception cref="System.RankException">arr is multidimensional.</exception>         public static int IndexOf(this Array arr, object val)         {             return Array.IndexOf(arr, val);         }         /// <summary>         /// Determines if the array contains the given value.         /// </summary>         /// <param name="arr">The one-dimensional System.Array to search.</param>         /// <param name="val">The object to locate in array.</param>         /// <returns>         /// </returns>         /// <exception cref="System.ArgumentNullException">arr is null</exception>         /// <exception cref="System.RankException">arr is multidimensional.</exception>         public static bool Contains(this Array arr, object val)         {             if (Array.IndexOf(arr, val) < arr.GetLowerBound(0))                 return false;             else                 return true;         }         /// <summary>         /// Copies the array to a new array of the same type.         /// </summary>         /// <param name="arr"></param>         /// <returns></returns>         public static Array Copy(this Array arr)         {             var newArr = Array.CreateInstance(arr.GetType().GetElementType(), arr.Length);             arr.CopyTo(newArr, 0);             return newArr;         }    } }