none
Delegates and Lambda Expressions RRS feed

  • Question

  • Hello Friends,

    I was wondering do I need to refactor this codes that is using a delegate with a lambda expression, or is this best practice? Thanks !

     

    namespace Delagate
    {
        delegate bool MeDelagate(int n);
    
        class Program
        {
            //static bool LessThanFive(int n) { return n < 5; }
           // static bool LessThanTen(int n) { return n < 10; }
            //static bool GreaterThanThirteen(int n) { return n > 13; }
    
            static void Main(string[] args)
            {
                int[] numbers = new[] { 2, 7, 3, 7, 9, 17,5, 1, 8, 1700 };
                IEnumerable<int> result = RunNumbersThroughGauntlet(numbers, n => n > 1500);
                foreach (int n in result)
                    Console.WriteLine(n);
            }
            static IEnumerable<int> RunNumbersThroughGauntlet(IEnumerable<int> numbers, MeDelagate gauntlet)
            {
                foreach (int number in numbers)
                    if (gauntlet(number))
                        yield return number;
            }
        }
    }



    • Edited by Ronald Rex Thursday, December 27, 2018 7:28 PM
    Thursday, December 27, 2018 7:12 PM

Answers

  • I don't see anything wrong here. Your other option would be 

    using System;
    using System.Collections.Generic;
    
    namespace Delagate
    {
        namespace Delagate
        {
            class Program
            {
                static void Main(string[] args)
                {
                    int[] numbers = { 2, 7, 3, 7, 9, 17, 5, 1, 8, 1700 };
                    IEnumerable<int> result = RunNumbersThroughGauntlet(numbers, number => number > 1500);
                    foreach (int n in result)
                    {
                        Console.WriteLine(n);
                    }
    
                    Console.ReadLine();
                }
                static IEnumerable<int> RunNumbersThroughGauntlet(IEnumerable<int> numbers, Func<int, bool> myMethodName)
                {
                    foreach (int number in numbers)
                    {
                        if (!myMethodName(number)) continue;
                        yield return number;
                    }
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ronald Rex Friday, December 28, 2018 4:20 AM
    Friday, December 28, 2018 2:08 AM
    Moderator
  • You can also use some existing things, such as :

       IEnumerable<int> result = numbers.Where( n => n > 1500 );

     

    or

       IEnumerable<int> result = numbers.Where( LessThanFive );

    • Marked as answer by Ronald Rex Friday, December 28, 2018 11:58 AM
    Friday, December 28, 2018 6:19 AM
  • So is it fair for me to say that lambda expressions are used primarily in linq? or can you think of some other real world application uses for them outside of linq. Thanks !
    Personally I use lambda syntax unless there is a compelling reason to use linq which is when I'm creating a complex statement that may need a let clause.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ronald Rex Friday, December 28, 2018 6:31 PM
    Friday, December 28, 2018 12:58 PM
    Moderator

All replies

  • I don't see anything wrong here. Your other option would be 

    using System;
    using System.Collections.Generic;
    
    namespace Delagate
    {
        namespace Delagate
        {
            class Program
            {
                static void Main(string[] args)
                {
                    int[] numbers = { 2, 7, 3, 7, 9, 17, 5, 1, 8, 1700 };
                    IEnumerable<int> result = RunNumbersThroughGauntlet(numbers, number => number > 1500);
                    foreach (int n in result)
                    {
                        Console.WriteLine(n);
                    }
    
                    Console.ReadLine();
                }
                static IEnumerable<int> RunNumbersThroughGauntlet(IEnumerable<int> numbers, Func<int, bool> myMethodName)
                {
                    foreach (int number in numbers)
                    {
                        if (!myMethodName(number)) continue;
                        yield return number;
                    }
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ronald Rex Friday, December 28, 2018 4:20 AM
    Friday, December 28, 2018 2:08 AM
    Moderator
  • You can also use some existing things, such as :

       IEnumerable<int> result = numbers.Where( n => n > 1500 );

     

    or

       IEnumerable<int> result = numbers.Where( LessThanFive );

    • Marked as answer by Ronald Rex Friday, December 28, 2018 11:58 AM
    Friday, December 28, 2018 6:19 AM
  • So is it fair for me to say that lambda expressions are used primarily in linq? or can you think of some other real world application uses for them outside of linq. Thanks !
    Friday, December 28, 2018 12:03 PM
  • So is it fair for me to say that lambda expressions are used primarily in linq? or can you think of some other real world application uses for them outside of linq. Thanks !
    Personally I use lambda syntax unless there is a compelling reason to use linq which is when I'm creating a complex statement that may need a let clause.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ronald Rex Friday, December 28, 2018 6:31 PM
    Friday, December 28, 2018 12:58 PM
    Moderator