You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
downkyi/DownKyi.Core/Utils/ListHelper.cs

59 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace DownKyi.Core.Utils
{
public static class ListHelper
{
/// <summary>
/// 判断ObservableCollection中是否存在不存在则添加
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
public static void AddUnique<T>(ObservableCollection<T> list, T item)
{
if (!list.Contains(item))
{
list.Add(item);
}
}
/// <summary>
/// 判断List中是否存在不存在则添加
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
public static void AddUnique<T>(List<T> list, T item)
{
if (!list.Exists(t => t.Equals(item)))
{
list.Add(item);
}
}
/// <summary>
/// 判断List中是否存在不存在则添加
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
/// <param name="index"></param>
public static void InsertUnique<T>(List<T> list, T item, int index)
{
if (!list.Exists(t => t.Equals(item)))
{
list.Insert(index, item);
}
else
{
list.Remove(item);
list.Insert(index, item);
}
}
}
}