获取除C#中特定值之外的所有数组元素

时间:2017-05-05 07:26:24

标签: c# unity3d

有没有办法让所有数组元素除了以获取用户提供的特定值?

我在Unity程序中使用C#语言。

2 个答案:

答案 0 :(得分:2)

这样的事情会起作用:

  // remove where not is "1"
  string[] arr = new[] { "1", "2", "3" };
  string[] all = arr.Where(x => (x != "1")).ToArray();

  // or remove by index
  int numIndex = Array.IndexOf(arr, "1");
  arr = arr.Where((val, idx) => idx != numIndex).ToArray();

答案 1 :(得分:1)

你可以这样做

public T[] Except<T>(T[] array, T specificValue) where T : IComparable {
            return array.Where<T>(val => val.CompareTo(specificValue) != 0).ToArray();
        }