공통 액세스 방법 집합 - Enumerable
17426 단어 enum
여기에 바로 코드를 열거했다.
public static class EnumerableExtension
{
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
Expect.ArgumentNotNull(action, "action");
foreach (T t in enumerable)
{
action(t);
}
}
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T, int> action)
{
Expect.ArgumentNotNull(action, "action");
int i = 0;
foreach (T t in enumerable)
{
action(t, i++);
}
}
/// <summary> Join the enumerable to a string.</summary>
public static string Join<T>(this IEnumerable<T> enumerable, string start, string end, string seperator)
{
return Join<T>(enumerable, start, end, seperator, e => e);
}
/// <summary> Join the enumerable to a string.</summary>
public static string Join<T>(this IEnumerable<T> enumerable, string start, string end, string seperator, Func<T, object> converter)
{
Expect.ArgumentNotNull(converter, "converter");
StringBuilder sb = new StringBuilder();
sb.Append(start);
enumerable.ForEach((e, i) => sb.AppendFormat("{0}{1}", i == 0 ? string.Empty : seperator, converter(e)));
sb.Append(end);
return sb.ToString();
}
/// <summary>
/// Generate an enumerable by repeating the given delegate.
/// </summary>
public static IEnumerable<T> Repeat<T>(this Func<T> generateResultFunc, int count)
{
for (int i = 0; i < count; ++i)
{
yield return generateResultFunc();
}
}
public static IDictionary<TKey, TValue> Clone<TKey, TValue>(this IDictionary<TKey, TValue> source)
{
Dictionary<TKey, TValue> dict = source as Dictionary<TKey, TValue>;
if (dict != null)
{
return new Dictionary<TKey, TValue>(source, dict.Comparer);
}
SortedDictionary<TKey, TValue> sortedDict = source as SortedDictionary<TKey, TValue>;
if (sortedDict != null)
{
return new SortedDictionary<TKey, TValue>(source, sortedDict.Comparer);
}
SortedList<TKey, TValue> sortedList = source as SortedList<TKey, TValue>;
if (sortedList != null)
{
return new SortedList<TKey, TValue>(source, sortedList.Comparer);
}
return new Dictionary<TKey, TValue>(source);
}
public static IList<T> Clone<T>(this IList<T> source)
{
return new List<T>(source);
}
public static Stack<T> Clone<T>(this Stack<T> source)
{
return new Stack<T>(source.Reverse());
}
public static Queue<T> Clone<T>(this Queue<T> source)
{
return new Queue<T>(source);
}
/// <summary>Merge the two dictionary. </summary>
public static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right)
{
Expect.ArgumentNotNull(left, "left");
Expect.ArgumentNotNull(right, "right");
foreach (var kvp in right)
{
left[kvp.Key] = kvp.Value;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Rails5에서 enum을 사용할 때는 _prefix (접두사) _suffix (접미사)를 사용합시다.Rails5에서 ActiveRecord::Enum를 정의 할 때 _prefix, _suffix라는 무기가 있다는 것을 알고 싶습니다. You can use the :_prefix or :_suffix options ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.