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 512845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:21:41+00:00 2026-05-13T07:21:41+00:00

Consider the following code snippet: class Test { public int Length{ get; set; }

  • 0

Consider the following code snippet:

class Test
{
    public int Length{ get; set; }
    public Func<List<int>, double> Calculate { get; set; }
    public void DoStuff(List<int> list)
    {
        // ... do some processing here ...
        double ans = Calculate(list);
        // ... do some more stuff ......
    }
}

Now, I have realized that the the typical anonymous Func delegate that will be attached to the Calculate property, would have something to do with the Length property of the Test class, and the length of the list which is being passed to the delegate. For some relationship between these two lengths (e.g., this.Length < list.Count), I should be able to throw an exception. But, this has to happen at runtime, and I would have no control over what the users of this class would be adding as delegate in the Calculate property.

How to do this? Can I do something in the set method of the Calculate property? How to check at runtime if certain condition involving the Length property of the Test class and the length (count) of the list object passed to the delegate is satisfied?

I would also consider design changes of the Test class to accomplish my objective (of runtime checking), but a way for the users to attach a Func delegate and still accomplish this would be neat.
Thanks.

  • 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-13T07:21:42+00:00Added an answer on May 13, 2026 at 7:21 am

    Add your own checks prior to invoking the supplied delegate. This way, you can check all of the requirements are met prior to invoking and the checking is done in one place (instead of duplicated in each implementation).

    public void DoStuff(List<int> list)
    {
        CheckPreconditions(list);
        double ans = Calculate(list);        
    }
    
    private void CheckPreconditions(List<int> list)
    {
        if(list == null)
           // throw ...
    
        if(list.Length == 0)
           // throw ...
    
        if(Length < list.Count)
            throw ArgumentException("list", 
                 "list must have blah blah blah to satisfy requirements");
    }
    

    This is similar to the template method pattern except instead of using inheritance, you’re delegating to a user-supplied function 🙂

    Also, if you make Calculate a field instead of a public property, you can force it to be executed through the DoStuff method, so the checks will always be carried out.

    Something like:

    class Test
    {
        public int Length{ get; set; }
        private Func<List<int>, double> m_calculate;
    
        // Inject into ctor
        public Test(Func<List<int>, double> calculate)
        {  
            if(calculate == null) throw new ArgumentNullException("calculate");
    
            m_calculate = calculate;
        }
    
        // all access to Calculate is now through this method
        public void DoStuff(List<int> list)
        {
            CheckPreconditions(list);
            double ans = m_calculate(list);        
        }
    }
    

    This would be a good fit if Calculate is only ever set once.

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

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like I'm reinventing the wheel. I'll take a… May 14, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer You need pass in second argument the path where you… May 14, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer Google has their own malloc replacement library at http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools with… May 14, 2026 at 10:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.