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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:05:00+00:00 2026-06-15T01:05:00+00:00

I have relativly often methods where i only want filled parameters as input. If

  • 0

I have relativly often methods where i only want filled parameters as input. If somebody calls the method, and the parameters are not correct, an error should be thrown.

Is there a way to annotate a method to say: only certain value ranges are allowed, or they should be not null?

With generics there is something like the “where” clause for restrictions (whihc go not so far to have values).

So I want to do instead of

private static void DoSomething(string string_in, object object_in,... ) 
{
    if (null == object_in) throw new NullReferenceException("Input parameter of object is empty.");
    if (String.IsNullOrEmpty(string )) throw new NullReferenceException("Input parameter string  is empty.");

something like

private static void DoSomething(string string_in, object object_in,... ) 
    where string _in:!String.IsNullOrEmpty(string_in)
    where object_in : object_in !=null

or

private static void DoSomething(string string_in != null, object object_in != null,... ) 

or (which i would like most)

[Restriction string_in: value != null, value != empty]
[Restriction object_in: value != null]
[Restriction int_in: value inRange 3..9]
private static void DoSomething(string string _in, object object_in,... ) 

So in short Is there a better way to restrict called type to a certain amount of values, then just do manually over and over again comparision to something?

  • 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-15T01:05:01+00:00Added an answer on June 15, 2026 at 1:05 am

    Code contracts offer static analysis for your code, so it is quite close to what you need.
    As a bonus you can enable runtime checking also.

    From msdn:

    Code contracts provide a way to specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits. Object invariants describe the expected state for a class that is in a good state.

    Code contracts include classes for marking your code, a static analyzer for compile-time analysis, and a runtime analyzer. The classes for code contracts can be found in the System.Diagnostics.Contracts namespace.

    The benefits of code contracts include the following:

    • Improved testing: Code contracts provide static contract
      verification, runtime checking, and documentation generation.

    • Automatic testing tools: You can use code contracts to generate more meaningful unit tests by filtering out meaningless test
      arguments that do not satisfy preconditions.

    • Static verification: The static checker can decide whether there are any contract violations without running the program. It checks
      for implicit contracts, such as null dereferences and array bounds,
      and explicit contracts.

    • Reference documentation: The documentation generator augments existing XML documentation files with contract information. There are
      also style sheets that can be used with Sandcastle so that the
      generated documentation pages have contract sections.

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

Sidebar

Related Questions

So for example we have real life photo. how to get (relativly to image
I have a relatively simple HTML / JavaScript form. Such as this: <form method='POST'
I have a relatively simple object with a method that makes an HTTP request.
I have a block of code that will be called relatively often. Prior to
I have around 10k video streams that I want to monitor. There's going to
I often have code based on a specific well defined algorithm. This gets well
In my applications, I often have to use relative paths. For example, when I
I have a relatively small database that I would like to generate a diagram
I have a relatively large enum wherein each member represents a message type. A
I have a relatively simple question. Will using PHP guarantee that a form is

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.