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

  • SEARCH
  • Home
  • 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 4111138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:01:41+00:00 2026-05-20T22:01:41+00:00

How can I get a hash of a delegate function in C#. I want

  • 0

How can I get a hash of a delegate function in C#. I want to be able to tell if different delegates are being sent into my function. My code looks something like this:

public string GetContent(Func<string, bool> isValid)
{
// Do some work
SomeFunctionToHashAFunction(isValid)
}

I would use .GetHashCode() but the .NET framework doesn’t guarantee that these will be unique.

EDIT
I have some cached content that I’m validating, but I only want to validate it once. However, if the validation function changes, then I’ll need to re-validate the cached content. I’m not sure if the ObjectIdGenerator will work in this instance since I need to identify if two anonymous functions have the same implementation.

  • 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-05-20T22:01:41+00:00Added an answer on May 20, 2026 at 10:01 pm

    There is no (at least non completely hacky) way to hash anonymous function/delegate. Even if function implementation is the same, it might be a closure – so validation outcome might be different based on the context state. Consider this example:

    public class Validator
    {
        public string SomeState { get; set; }
    
        public Validator(string someState)
        {
            SomeState = someState;
        }
    
        public bool IsValid(string input)
        {
            return input == SomeState;
        }
    }
    
    // assume your 'input' being validated is "foo"
    GetContent((new Validator("foo")).IsValid); // IsValid returns true
    GetContent((new Validator("bar")).IsValid); // IsValid returns false
    

    So the only way be sure of whether the validation function is unique would be to have caller define uniqueness of validation implementation and have the caller pass that information to you. You would have to switch to using some kind of validator interface, something along these lines:

    //
    // Your code
    //
    
    public string GetContent(IValidator validator, 
        IEqualityComparer<IValidator> comparer)
    {
        // for tracking used validators, use instance 
        // of 'new HashSet<IValidator>(comparer)'
        // this will give you a hashset of unique validators
    }
    
    public interface IValidator
    {
        bool IsValid(string input);
    }
    
    //
    // Your callers code
    //
    
    public class Validator : IValidator
    {
        // same as Validator class code above
    }
    
    public class ValidatorEqualityComparer : IEqualityComparer<Validator>
    {
        public bool Equals(Validator v1, Validator v2)
        {
            return GetHashCode(v1) == GetHashCode(v2);
        }
    
        public int GetHashCode(Validator v)
        {
            int hCode = GetMyStringHash(v.GetType().GUID.ToString() + v.SomeState);
            // as for GetMyStringHash() implementation for this example, 
            // you can use some simple string hashing: 
            // http://www.techlicity.com/blog/dotnet-hash-algorithms.html
            return hCode;
        }
    }
    

    Then you can call your method like this:

    GetContent(new Validator("foo"), new ValidatorEqualityComparer());
    

    So the most important part to note here, is that when implementing ValidatorEqualityComparer.GetHashCode() you use validator object state (object value based) hashing. Only this will ensure true uniqueness of validation logic.

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

Sidebar

Related Questions

I know I can get the hash value directly with this bit of code:
How can I get the value of the URL hash (eg PARAMETERS in url
I create a GUID (as a string) and get the hash of it. Can
Logging can get complicated, quickly. Considering that you have some code, how do you
When calling each on a hash in ruby, you can get the key and
I can get all those url's whose content/type is text/html, but If I want
I want to get hash data in the redis server from nodeJs server i
I can't get window.location.hash = location.hash to work in Safari. I'm using javascript to
Using Cygwin Perl v5.8.8 and Win32::TieRegistry 0.26. We can get a tied hash object
I have some code in my application that looks something like this: char *hash

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.