Method with Func as parameter RRS feed

  • Question

  • User1836073717 posted

    How do i write a method for this test?

    public void Empty_keep()
    Assert.That(new LinkedList<int>().Keep(x => x < 10), Is.EqualTo(new LinkedList<int>()));

    I was thinking a extension method that takes a func as parameter?

    Something like:

    public static IEnumerable Keep(this Func<int, IEnumerable>) but i need some help?


    Sunday, October 26, 2014 12:33 PM


  • User1176121428 posted

    Hi iAndy,

    Thanks for your post.

    Here is an example that should work:

    public class Class1
        public int Method1(string input)
            //... do something
            return 0;
        public int Method2(string input)
            //... do something different
            return 1;
        public bool RunTheMethod(Func<string, int> myMethodName)
            //... do stuff
            int i = myMethodName("My String");
            //... do more stuff
            return true;
        public bool Test()
            return RunTheMethod(Method1);

    More information:

    Method using Func<T,TResult> as parameters

    Func<T, TResult> Delegate

    How to Pass Method or Function as Parameter in C# ?

    Hope this can be helpful.

    Best Regards,


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2014 11:07 PM