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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:16:21+00:00 2026-05-15T22:16:21+00:00

I’d like to restrict the value of a number parameter in a constructor to

  • 0

I’d like to restrict the value of a number parameter in a constructor to within a certain range.

I know the conventional way is to do something like the following:

public class Foo
{
    public int MaxAmount { get; }
    public int Amount { get; set; }

    public Foo(int amount)
    {
        if (amount > MaxAmount) { Amount = MaxAmount; }
        else if (amount < 1) { Amount = 1; }
        else { Amount = amount; }
    }
}

But what I don’t like about this is that the caller doesn’t know when the property gets set to something other than what was specified. I could return an exception instead of silently clamping the value, but that’s not very friendly.

What I’d like is something akin to this:

public Foo(int(1, this.MaxAmount) amount) // Where int(minimumValue, maximumValue)
{
   Amount = amount;
}

in which one wouldn’t even be able to instantiate Foo with an unacceptable value – the framework would prevent it.

Is anything like this possible?

EDIT FOR CLARITY:

What I’m after is a means by which the parameter itself can carry and communicate the information about its constraints – in a ‘baked in’ fashion which might, for example, surface in Intellisense when you wrote the call. So, I’d avoid the work of even attempting to instantiate the class if the values for the parameters were not valid.

If, for example, the program is running and the user types a number (N) and presses a button which creates a new Foo with an illegal quantity of N, I now have an exception to handle and something to debug and fix. Why even allow it in the first place? If Foo has been explicitly defined as having an upper boundary of 4 for its Amount property, what’s the point of allowing the developer to write Foo(5) when I could have informed him that the value he’s passing is not valid at the time that he wrote it?

If there’s some syntactic sugar, like ParameterConstraint or something, that is handled by the Framework for me so that I don’t have to roll my own into every class I write, I think that would be very useful.

  • 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-15T22:16:22+00:00Added an answer on May 15, 2026 at 10:16 pm

    You can do this using static-contract checking with Code Contracts (Premium only – The standard edition only offers runtime contract checking).

    The syntax is simply

    public Foo(int amount) {
        Contract.Requires(amount < MaxAmount);
        ...
     }
    

    (Requires) contracts are evaluated by checking that the arguments is constrained when calling the method. In your instance, it will be difficult to evaluate the constructor argument with against the instance field MaxAmount, because you cannot check that value beforehand. (Make MaxValue static to solve this).

    Example of such call.

    int val = _getFromSomewhere();
    var foo = new Foo(val); 
    //This May produce compile time error 
    // because the contract checker cannot prove you contract is met.
    

    The fix would be to make sure you put the constraint where you call is made.

    int val = _getFromSomewhere();
    if (val < Foo.MaxAmount)
        var foo = new Foo(val); 
        //Will always compile fine, because contract is met.
    

    When you install Contracts, the static checker isn’t turned on by default. Your project properties will have an extra tab where you can configure the contract options and enable static checking.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have text I am displaying in SIlverlight that is coming from a CMS
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.