【问题标题】:Is it possible to test an entire array at once? [duplicate]是否可以一次测试整个阵列? [复制]
【发布时间】:2020-03-20 01:57:12
【问题描述】:

有没有办法使用 if 语句通过执行以下操作来一次测试整个数组:

if(myArray == {1,2,3})
{Debug.Log("This is quick")}

或者我是否需要像这样遍历数组中的每个值:

if(myArray[0] == 1 && myArray[1] == 2 && myArray[2] == 3)
{Debug.Log("This is not as quick")}

【问题讨论】:

  • 你可以使用 SequenceEquals
  • 或者看看this one...
  • SequenceEqual 可以解决问题。谢谢大家!

标签: c# arrays if-statement


【解决方案1】:

您应该使用SequenceEqual

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        int [] myArray = {1,2,3};
        int [] myArray2 = {1,2,4};

        bool result = myArray.SequenceEqual(myArray2);
    }
}

【讨论】:

  • 这是完美的解决方案,让我的生活轻松了许多。谢谢!
猜你喜欢
  • 2018-10-30
  • 2011-01-25
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 2015-06-10
相关资源
最近更新 更多