Files
2026-02-21 16:45:37 +08:00

97 lines
1.8 KiB
C#

using System;
using System.Text;
namespace BitStrap
{
public static class ArrayExtensions
{
public static int Count<T>(this T[] collection, Predicate<T> predicate)
{
if (predicate == null)
{
return 0;
}
int num = 0;
for (int i = 0; i < collection.Length; i++)
{
if (predicate(collection[i]))
{
num++;
}
}
return num;
}
public static bool All<T>(this T[] collection, Predicate<T> predicate)
{
if (predicate == null)
{
return false;
}
for (int i = 0; i < collection.Length; i++)
{
if (!predicate(collection[i]))
{
return false;
}
}
return true;
}
public static bool Any<T>(this T[] collection, Predicate<T> predicate)
{
if (predicate == null)
{
return false;
}
for (int i = 0; i < collection.Length; i++)
{
if (predicate(collection[i]))
{
return true;
}
}
return false;
}
public static T FirstOrDefault<T>(this T[] collection)
{
return (collection.Length <= 0) ? default(T) : collection[0];
}
public static T FirstOrDefault<T>(this T[] collection, Predicate<T> predicate)
{
for (int i = 0; i < collection.Length; i++)
{
if (predicate(collection[i]))
{
return collection[i];
}
}
return default(T);
}
public static string ToStringFull<T>(this T[] collection)
{
if (collection == null)
{
return "null";
}
if (collection.Length <= 0)
{
return "[]";
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("[ ");
for (int i = 0; i < collection.Length - 1; i++)
{
stringBuilder.Append(collection[i].ToString());
stringBuilder.Append(", ");
}
stringBuilder.Append(collection[collection.Length - 1].ToString());
stringBuilder.Append(" ]");
return stringBuilder.ToString();
}
}
}