[C#] Better Linq Extensions

Results 1 to 9 of 9
  1. #1
    Alpha Member Emily is offline
    MemberRank
    Oct 2012 Join Date
    The NetherlandsLocation
    2,408Posts

    [C#] Better Linq Extensions

    Hello, today I release some custom System.Linq extensions which works better because it uses foreach except of while. For now it only works with arrays (like int[] and string[]), dictionary and list, and I've only created ForEach and While (using foreach loop, not while loop)

    For using it, you need to remove this at the using System; block:

    PHP Code:
    using System.Linq
    And add:

    PHP Code:
    using CustomLinqExtensions
    Then add this class to your C# Project:

    PHP Code:
    using System;
    using System.Collections.Generic;

    namespace 
    CustomLinqExtensions
    {
        public static class 
    LinqExtensions
        
    {
            
    #region ForEach
            
    public static void ForEach<T>(this List<TitemsAction<Taction)
            {
                foreach (var 
    Item in items)
                {
                    
    action.Invoke(Item);
                }
            }

            public static 
    void ForEach<TKeyTValue>(this Dictionary<TKeyTValueitemsAction<TKeyTValueaction)
            {
                foreach (var 
    Item in items)
                {
                    
    action.Invoke(Item.KeyItem.Value);
                }
            }

            public static 
    void ForEach<T>(this T[] itemsAction<Taction)
            {
                foreach (var 
    Item in items)
                {
                    
    action.Invoke(Item);
                }
            }
            
    #endregion

            #region While
            
    public static List<T> While<T>(this List<TitemsFunc<Tboolexpression)
            {
                List<
    TItems = new List<T>();

                foreach (var 
    Item in items)
                {
                    if (
    expression.Invoke(Item))
                        
    Items.Add(Item);
                }

                return 
    Items;
            }

            public static 
    Dictionary<TKeyTValue> While<TKeyTValue>(this Dictionary<TKeyTValueitemsFunc<TKeyTValueboolexpression)
            {
                
    Dictionary<TKeyTValueItems = new Dictionary<TKeyTValue>();

                foreach (var 
    Item in items)
                {
                    if (
    expression.Invoke(Item.KeyItem.Value))
                        
    Items.Add(Item.KeyItem.Value);
                }

                return 
    Items;
            }

            public static 
    T[] While<T>(this T[] itemsFunc<Tboolexpression)
            {
                List<
    TItems = new List<T>();

                foreach (var 
    Item in items)
                {
                    if (
    expression.Invoke(Item))
                        
    Items.Add(Item);
                }

                return 
    Items.GetArray();
            }
            
    #endregion

            #region GetArray
            
    public static T[] GetArray<T>(this List<Titems)
            {
                
    T[] Array = new T[items.Count];
                
    int pos 0;

                foreach (var 
    item in items)
                    Array[
    pos++] = item;

                return Array;
            }
            
    #endregion
        
    }

    NOTE:
    With the List<int> etc you need to use ForEach<TYPE>(....) because List always uses the ForEach() method which is already implemented.

    Another note, I'll explain something:

    ForEach just replaces for example this:

    PHP Code:
    foreach (var i in new int[] { 1234})
    {
    Console.WriteLine(i);

    While just replaces for example this:

    PHP Code:
    List<intvals = new List<int>();

    foreach (var 
    i in new int[] { 1234})
    {
    if (
    2)
    vals.Add(i);
    }

    return 
    vals
    GetArray just pushes every value of a LIST (only a list for now) into an array of the specified type.

    For using you only have to use type argument if you use list, since list has implemented ForEach. If you still want to use ForEach with my method, you can create a new ForEach extension for IList which doesn't have implemented ForEach (I think)

    Credits:

    Me 95% - Creating code
    MS 5% - Giving explaination


  2. #2
    Developer nevS is offline
    MemberRank
    Aug 2005 Join Date
    GermanyLocation
    531Posts

    Re: [C#] Better Linq Extensions

    Quote Originally Posted by Tha View Post
    Hello, today I release some custom System.Linq extensions which works better because it uses foreach except of while.
    And why is it better? I don't see the point
    Your extensions also are limited to specific types, while it's better to code against interfaces.

    ForEach-Extensions:
    I would just make one for IEnumerable<T>.

    While-Extensions:
    I don't get why you named them "While", when they don't stop when the expression returns false.
    What you want to achieve with this code is already in System.Linq-Extensions, it would be .Where(...) or .TakeWhile(...)

    GetArray-Extension:
    Already implemented in System.Linq-Extensions, called ToArray(). There are also ToList() and ToDictionary(...)


    Examples to replace your examples:
    ForEach just replaces for example this:

    PHP Code:
    foreach (var i in new int[] { 1234})
    {
    Console.WriteLine(i);

    Code:
    Enumerable.Range(1, 5).ForEach(Console.WriteLine);
    While just replaces for example this:

    PHP Code:
    List<intvals = new List<int>();

    foreach (var 
    i in new int[] { 1234})
    {
    if (
    2)
    vals.Add(i);
    }

    return 
    vals
    your example doesn't even compile ;) However if you want each value when the modulo returns 0 as list:
    Code:
    var values = Enumerable.Range(1, 5).Where(i => i % 2 == 0).ToList();
    I recommend reading the 101 LINQ Samples in MSDN
    Last edited by nevS; 09-03-13 at 12:59 PM.

  3. #3
    Alpha Member Emily is offline
    MemberRank
    Oct 2012 Join Date
    The NetherlandsLocation
    2,408Posts

    Re: [C#] Better Linq Extensions

    Quote Originally Posted by nevS View Post
    And why is it better? I don't see the point
    Your extensions also are limited to specific types, while it's better to code against interfaces.

    ForEach-Extensions:
    I would just make one for IEnumerable<T>.

    While-Extensions:
    I don't get why you named them "While", when they don't stop when the expression returns false.
    What you want to achieve with this code is already in System.Linq-Extensions, it would be .Where(...) or .TakeWhile(...)

    GetArray-Extension:
    Already implemented in System.Linq-Extensions, called ToArray(). There are also ToList() and ToDictionary(...)


    Examples to replace your examples:

    Code:
    Enumerable.Range(1, 5).ForEach(Console.WriteLine);
    your example doesn't even compile ;) However if you want each value when the modulo returns 0 as list:
    Code:
    var values = Enumerable.Range(1, 5).Where(i => i % 2 == 0).ToList();
    I recommend reading the 101 LINQ Samples in MSDN
    Microsoft's LINQ expressions use:

    using .......

  4. #4
    Developer nevS is offline
    MemberRank
    Aug 2005 Join Date
    GermanyLocation
    531Posts

    Re: [C#] Better Linq Extensions

    Quote Originally Posted by Tha View Post
    Microsoft's LINQ expressions use:

    using .......
    So, what's the problem?

  5. #5
    Alpha Member Emily is offline
    MemberRank
    Oct 2012 Join Date
    The NetherlandsLocation
    2,408Posts

    Re: [C#] Better Linq Extensions

    Quote Originally Posted by nevS View Post
    So, what's the problem?
    Using is slower than foreach.

  6. #6
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [C#] Better Linq Extensions

    "using" doesn't have any overhead (after optimizations). using(var v) just instantiates v and then calls v.Dispose() when it falls out of the using scope. It doesn't look any different in IL than manually instantiating/disposing variables. You could say that Dispose() is slower, but that depends what you're iterating over and in general if you're stressing over a using versus a foreach block you're missing the point of LINQ. LINQ isn't supposed to be terribly fast, but it is supposed to be a quick way to query over objects. Your code doesn't even query, it just copies IEnumerables based on a Predicate<T>.

  7. #7
    Alpha Member Emily is offline
    MemberRank
    Oct 2012 Join Date
    The NetherlandsLocation
    2,408Posts

    Re: [C#] Better Linq Extensions

    Sorry guys I meant Microsoft's LINQ Extensions use a lot of while statements and they're slower than Foreach..
    So I meant while except of using..

  8. #8
    Developer nevS is offline
    MemberRank
    Aug 2005 Join Date
    GermanyLocation
    531Posts

    Re: [C#] Better Linq Extensions

    Quote Originally Posted by Tha View Post
    Using is slower than foreach.
    Well, guess what your foreach-loop does in the background. It also creates an enumurator-object which gets disposed after your loop finished. Your code is creating additional overhead by creating new objects (lists, arrays), so how it's gonna be faster?
    I would not want to sacrifice the elegant and powerful Linq-Extensions, in the most cases the difference to really optimized code does not even matter.

  9. #9
    Chasing 99 Red Balloons Jordan is offline
    MemberRank
    Jan 2008 Join Date
    UKLocation
    1,763Posts

    Re: [C#] Better Linq Extensions

    Tha, if you care about speed why are you using Linq? From my understanding it's mostly a JIT/Runtime feature so it's always going to be slower. Feel free to correct me if i'm wrong :)



Advertisement