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

43 lines
1.1 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;
namespace DownKyi.Core.Utils
{
public static class ListHelper
{
/// <summary>
/// 判断List中是否存在不存在则添加
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></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);
}
}
}
}