csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
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.
81 lines
2.1 KiB
81 lines
2.1 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="IEnumerableUtils.cs" company="Steven Kirk">
|
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex.Controls.Utils
|
|
{
|
|
using System;
|
|
using System.Collections;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
internal static class IEnumerableUtils
|
|
{
|
|
public static bool Contains(this IEnumerable items, object item)
|
|
{
|
|
return items.IndexOf(item) != -1;
|
|
}
|
|
|
|
public static int Count(this IEnumerable items)
|
|
{
|
|
Contract.Requires<ArgumentNullException>(items != null);
|
|
|
|
var collection = items as ICollection;
|
|
|
|
if (collection != null)
|
|
{
|
|
return collection.Count;
|
|
}
|
|
else
|
|
{
|
|
return Enumerable.Count(items.Cast<object>());
|
|
}
|
|
}
|
|
|
|
public static int IndexOf(this IEnumerable items, object item)
|
|
{
|
|
Contract.Requires<ArgumentNullException>(items != null);
|
|
|
|
var list = items as IList;
|
|
|
|
if (list != null)
|
|
{
|
|
return list.IndexOf(item);
|
|
}
|
|
else
|
|
{
|
|
int index = 0;
|
|
|
|
foreach (var i in items)
|
|
{
|
|
if (object.ReferenceEquals(i, item))
|
|
{
|
|
return index;
|
|
}
|
|
|
|
++index;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public static object ElementAt(this IEnumerable items, int index)
|
|
{
|
|
Contract.Requires<ArgumentNullException>(items != null);
|
|
|
|
var list = items as IList;
|
|
|
|
if (list != null)
|
|
{
|
|
return list[index];
|
|
}
|
|
else
|
|
{
|
|
return items.Cast<object>().ElementAt(index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|