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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:54:40+00:00 2026-05-17T21:54:40+00:00

Say I have the following public T Example(Func<T> f) { Contract.Requires(f != null); Contract.Requires(f()

  • 0

Say I have the following

public T Example(Func<T> f)
{
     Contract.Requires(f != null);
     Contract.Requires(f() != null); // no surprise, this is an error
...
}

Is there any way to specify that my Func<T> parameters must obey some contracts?

  • 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-17T21:54:41+00:00Added an answer on May 17, 2026 at 9:54 pm

    In general, that seems like a problematic requirement since invoking f may introduce side effects so specifying a contract could affect what the method does.

    As the implementer of the Code Contracts library one could introduce wrapper code that checks to see if the value of f obeys the contract when it is invoked in the context of your method (to avoid introducing a spurious method call). This is problematic for a couple of reasons:

    1. The method might never invoke f, so the caller could violate the contract and not get busted.
    2. The method could call f only after doing some other work which might be invalid given that the call to f didn’t satisfy the specification.

    If f had no side effects then these wouldn’t be problems, but in the presence of side effects dealing with 1 by always calling f wouldn’t work and calling f before doing any work to deal with 2 also wouldn’t fly.

    So, in conclusion I don’t think that this is possible (in the context of native code contracts) and with good reason.

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

Sidebar

Related Questions

Say suppose I have the following Java code. public class Example { public static
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say I have the following two classes: package example.model; public class Model {
Say for example, I have the following: public interface IPet { long? Id {
Is this even possible? For example, let's say I have the following: class Window
Let's say that I have the following delegate: public delegate void Example(); and a
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
Say for example I have the following type: public class Site { public string
Say I have the following example: #include <cstdlib> class A { public: static const
Lets say I have the following class: public class Provider { ... public sealed

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.