Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:40:00+00:00 2026-06-05T21:40:00+00:00

I have a method that accepts a generic delegate as a parameter, and inserts

  • 0

I have a method that accepts a generic delegate as a parameter, and inserts it in a list:

public void AddFilterMember<T>(Func<T, bool> filterMember)
{
    filter.Add(filterMember);
}

Later on all delegates are invoked over an instance of type T to find out if this instance passes the filter, i.e, if true is returned for every filterMember invoked.

I noticed that it is possible to pass an invalid lambda expression like the following:

string str = null;
AddFilterMember(x => str.Contains((string)x));

Which obviously will throw an exception when invoked because the str string is null. So I would like to know the best way to validate a lambda expression against null references (other than its parameters) at the moment it is defined?

I guess one option would be to invoke it using a default instance of T, but sometimes this is not feasible because T may not have a default parameterless constructor.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:40:02+00:00Added an answer on June 5, 2026 at 9:40 pm

    Here’s a possible solution:

    public bool AddFilterMember<T>(Func<T, bool> filterMember, T checkValue = default(T))
    {
        try
        {
            filterMember(checkValue);
        }
        catch
        {
            return false;
        }
        filter.Add(filterMember);
        return true;
    }
    

    If the caller knows that default(T) won’t work, but expects all actually-used values to work, they can specify the checkValue to be an example. From there, you just try to run the delegate and see if it works or not. A bool is returned to let the caller know whether it was successfully done or not.

    Note that invoking the delegate can cause side effects. The behavior of this should be documented so callers aren’t surprised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method Foo4 that accepts a parameter of the type Func<>. If
I have a generic method that accepts a Func<int> and I would like to
I have a method that accepts a parameter obj of type System.Object Now I
I have a repository method that accepts an order by parameter in the form:
I have a method that accepts an IEnumerable-decimals and performance various math functions. I
I have a method that accepts a sender and is called with a UIbutton.
I have a method that prints private variable of the class. public class Test
The lastIndexOf() method of List interface accepts a parameter which is of type Object.
I have a class Exporter which has a generic method which accepts an IEnumerable<T>
say, I have a generic command trait with an execute method that takes an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.