, . .
, . , , , .
for (int index = 0; index < items.Length; index++)
if (items[index] == "one")
items[index] = "zero";
Simple.
, , :
void ReplaceAll(string[] items, string oldValue, string newValue)
{
for (int index = 0; index < items.Length; index++)
if (items[index] == oldValue)
items[index] = newValue;
}
:
ReplaceAll(items, "one", "zero");
:
static class ArrayExtensions
{
public static void ReplaceAll(this string[] items, string oldValue, string newValue)
{
for (int index = 0; index < items.Length; index++)
if (items[index] == oldValue)
items[index] = newValue;
}
}
:
items.ReplaceAll("one", "zero");
, :
static class ArrayExtensions
{
public static void ReplaceAll<T>(this T[] items, T oldValue, T newValue)
{
for (int index = 0; index < items.Length; index++)
if (items[index].Equals(oldValue))
items[index] = newValue;
}
}
.
. , , , . , IEqualityComparer<T>, , ; , T string - :
static class ArrayExtensions
{
public static void ReplaceAll<T>(this T[] items, T oldValue, T newValue)
{
items.ReplaceAll(oldValue, newValue, EqualityComparer<T>.Default);
}
public static void ReplaceAll<T>(this T[] items, T oldValue, T newValue, IEqualityComparer<T> comparer)
{
for (int index = 0; index < items.Length; index++)
if (comparer.Equals(items[index], oldValue))
items[index] = newValue;
}
}