Что является лучшим способом проверить два Списка <T> списки для равенства в C#

Вы можете использовать Element.querySelector для получения диапазона, а затем удалять / добавлять к нему классы, используя его свойство classList .

var span = btn.querySelector('.glyphicon');
span.classList.remove('glyphicon-volume-off', 'glyphicon-volume-up');
span.classList.add('glyphicon-' + value);

Рабочая демонстрация (смотрите кнопку отключения звука под видео):

var mediaPlayer = document.getElementById('video');
var muteBtn = document.getElementById('mute-button');

function changeButtonType(btn, value) {
  btn.title = value;
  btn.className = value;
  btn.setAttribute("aria-label", value);

  var span = btn.querySelector('.glyphicon');
  span.classList.remove('glyphicon-volume-off', 'glyphicon-volume-up');
  span.classList.add('glyphicon-' + value);
}

function toggleMute() {
  if (mediaPlayer.muted) {
    // Change the cutton to be a mute button
    changeButtonType(muteBtn, 'volume-off');
    // Unmute the media player
    mediaPlayer.muted = false;
  } else {
    // Change the button to be an unmute button
    changeButtonType(muteBtn, 'volume-up');
    // Mute the media player
    mediaPlayer.muted = true;
  }
}


18
задан Rami Shareef 17 July 2011 в 10:39
поделиться

4 ответа

Enumerable.SequenceEqual<TSource>

MSDN

40
ответ дан 30 November 2019 в 06:29
поделиться

Evil implementation is

if (List1.Count == List2.Count)
{
   for(int i = 0; i < List1.Count; i++)
   {
      if(List1[i] != List2[i])
      {
         return false;
      }
   }
   return true;
}
return false;
3
ответ дан 30 November 2019 в 06:29
поделиться

I put together this variation:

private bool AreEqual<T>(List<T> x, List<T> y)
{
    // same list or both are null
    if (x == y)
    {
        return true;
    }

    // one is null (but not the other)
    if (x== null || y == null)
    {
        return false;
    }

    // count differs; they are not equal
    if (x.Count != y.Count)
    {
        return false;
    }

    for (int i = 0; i < x.Count; i++)
    {
        if (!x[i].Equals(y[i]))
        {
            return false;
        }
    }
    return true;
}

The nerd in me also crawled out so I did a performance test against SequenceEquals, and this one has a slight edge.

Now, the question to ask; is this tiny, almost measurable performance gain worth adding the code to the code base and maintaining it? I very much doubt it ;o)

3
ответ дан 30 November 2019 в 06:29
поделиться

I knocked up a quick extension method:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static bool Matches<T>(this List<T> list1, List<T> list2)
        {
            if (list1.Count != list2.Count) return false;
            for (var i = 0; i < list1.Count; i++)
            {
                if (list1[i] != list2[i]) return false;
            }
            return true;
        }
    }   
}
2
ответ дан 30 November 2019 в 06:29
поделиться
Другие вопросы по тегам:

Похожие вопросы: