Enumerable IndexSet

A Enumerable static Extension to use Indexset.

A way to get the current index of a list or to see if its the first or last in the index.
IndexSet Entity is needed if you wanna get the current index. And KeyValuePairIndexSet is needed if you wanna use a Dictonary.
If you include both, then you can use the entire Static class.

IndexSet Entity.


namespace Your.Namespace
{
    public class IndexSet<T>
    {
        private readonly int _MaxIndex;

        public int Index { get; set; }
        public T Value { get; set; }

        public bool IsFIrst
        {
            get
            {
                return Index.Equals(1);
            }
        }
        public bool IsLast
        {
            get
            {
                return Index.Equals(_MaxIndex);
            }
        }
        public bool IsFirstOrLast
        {
            get
            {
                return IsFIrst || IsLast;
            }
        }

        public IndexSet(T value, int index = 0, int max = 0)
        {
            Index = index;
            _MaxIndex = max - 1;
            Value = value;
        }
    }
}

KeyValuePairIndexSet Entity.


using System.Collections.Generic;

namespace Your.Namesapce
{
    public class KeyValuePairIndexSet<TKey, TValue>
    {
        private readonly int _MaxIndex;

        public int Index { get; set; }
        public TValue Value { get; set; }
        public TKey Key { get; set; }

        public bool IsFIrst
        {
            get
            {
                return Index.Equals(1);
            }
        }
        public bool IsLast
        {
            get
            {
                return Index.Equals(_MaxIndex);
            }
        }
        public bool IsFirstOrLast
        {
            get
            {
                return IsFIrst || IsLast;
            }
        }

        public KeyValuePairIndexSet(KeyValuePair<TKey, TValue> keyValuePair, int index = 0, int max = 0)
        {
            Index = index;
            _MaxIndex = max - 1;
            Value = keyValuePair.Value;
            Key = keyValuePair.Key;
        }
    }
}

Static EnumerableExtensions Helper.


using System.Collections.Generic;
using System.Linq;

namespace You.Namespace
{
public static class EnumerableExtensions
    {
        private static bool IsItem<T>(this T item, T itemToCompare)
        {
            if (itemToCompare == null)
            {
                return false;
            }
            return item.Equals(itemToCompare);
        }

        public static IEnumerable<IndexSet<T>> ToIndexSet<T>(this IEnumerable<T> enumerable)
        {
            var length = enumerable.Count();
            return enumerable.Select((value, index) => new IndexSet<T>(value, index, length));
        }
        public static List<IndexSet<T>> ToIndexSet<T>(this List list)
        {
            var length = list.Count();
            return list.Select((value, index) => new IndexSet<T>(value, index, length)).ToList();
        }
        public static IList<IndexSet<T>> ToIndexSet<T>(this IList iList)
        {
            var length = iList.Count();
            return iList.Select((value, index) => new IndexSet<T>(value, index, length)).ToList();
        }
        public static ICollection<IndexSet<T>> ToIndexSet<T>(this ICollection iCollection)
        {
            var length = iCollection.Count();
            return iCollection.Select((value, index) => new IndexSet<T>(value, index, length)).ToList();
        }
        public static List<KeyValuePairIndexSet<T, T>> ToIndexSet<T>(this Dictionary<T, T> dictionary)
        {
            var length = dictionary.Count();
            return dictionary.Select((value, index) => new KeyValuePairIndexSet<T, T>(value, index, length)).ToList();
        }
        public static IndexSet[] ToIndexSet<T>(this T[] array)
        {
            var length = array.Count();
            return array.Select((value, index) => new IndexSet<T>(value, index, length)).ToArray();
        }

        public static bool IsLast<T>(this IEnumerable<T> items, T item)
        {
            return item.IsItem(items.LastOrDefault());
        }
        public static bool IsLast<T>(this List<T> items, T item)
        {
            return item.IsItem(items.LastOrDefault());
        }
        public static bool IsLast<T>(this IList<T> items, T item)
        {
            return item.IsItem(items.LastOrDefault());
        }
        public static bool IsLast<T>(this ICollection<T> items, T item)
        {
            return item.IsItem(items.LastOrDefault());
        }
        public static bool IsLast<T>(this Dictionary<T, T> items, KeyValuePair<T, T> item)
        {
            return item.IsItem(items.LastOrDefault());
        }
        public static bool IsLast<T>(this T[] items, T item)
        {
            return item.IsItem(items.LastOrDefault());
        }

        public static bool IsFirst<T>(this IEnumerable<T> items, T item)
        {
            return item.IsItem(items.FirstOrDefault());
        }
        public static bool IsFirst<T>(this List<T> items, T item)
        {
            return item.IsItem(items.FirstOrDefault());
        }
        public static bool IsFirst<T>(this IList<T> items, T item)
        {
            return item.IsItem(items.FirstOrDefault());
        }
        public static bool IsFirst<T>(this ICollection<T> items, T item)
        {
            return item.IsItem(items.FirstOrDefault());
        }
        public static bool IsFirst<T>(this Dictionary<T, T> items, KeyValuePair<T, T> item)
        {
            return item.IsItem(items.FirstOrDefault());
        }
        public static bool IsFirst<T>(this T[] items, T item)
        {
            return item.IsItem(items.FirstOrDefault());
        }

        public static bool IsFirstOrLast<T>(this IEnumerable<T> items, T item)
        {
            return items.IsFirst(item) || items.IsLast(item);
        }
        public static bool IsFirstOrLast<T>(this List<T> items, T item)
        {
            return items.IsFirst(item) || items.IsLast(item);
        }
        public static bool IsFirstOrLast<T>(this IList<T> items, T item)
        {
            return items.IsFirst(item) || items.IsLast(item);
        }
        public static bool IsFirstOrLast<T>(this ICollection<T> items, T item)
        {
            return items.IsFirst(item) || items.IsLast(item);
        }
        public static bool IsFirstOrLast<T>(this Dictionary<T, T> items, KeyValuePair<T, T> item)
        {
            return items.IsFirst(item) || items.IsLast(item);
        }
        public static bool IsFirstOrLast<T>(this T[] items, T item)
        {
            return items.IsFirst(item) || items.IsLast(item);
        }
    }

Exsample of how to use it.


// A simpe list. You can also use your own objects.
var list = new List()
{
    "First",
    "Middle",
    "Last"
};

// Whitout IndexSet
foreach (var s in list)
{
    bool isFirst = list.IsFirst(s);
    string value = s;
};

// Converted to IndexSet. Now your object is stored in Value.
foreach (var s in list.ToIndexSet())
{
    bool isFirst = s.IsFIrst;
    string value = s.Value;
};