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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:55:52+00:00 2026-05-10T20:55:52+00:00

Is there a simple attribute or data contract that I can assign to a

  • 0

Is there a simple attribute or data contract that I can assign to a function parameter that prevents null from being passed in C#/.NET? Ideally this would also check at compile time to make sure the literal null isn’t being used anywhere for it and at run-time throw ArgumentNullException.

Currently I write something like …

if (null == arg)   throw new ArgumentNullException('arg'); 

… for every argument that I expect to not be null.

On the same note, is there an opposite to Nullable<> whereby the following would fail:

NonNullable<string> s = null; // throw some kind of exception 
  • 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. 2026-05-10T20:55:53+00:00Added an answer on May 10, 2026 at 8:55 pm

    There’s nothing available at compile-time, unfortunately.

    I have a bit of a hacky solution which I posted on my blog recently, which uses a new struct and conversions.

    In .NET 4.0 with the Code Contracts stuff, life will be a lot nicer. It would still be quite nice to have actual language syntax and support around non-nullability, but the code contracts will help a lot.

    I also have an extension method in MiscUtil called ThrowIfNull which makes it a bit simpler.

    One final point – any reason for using ‘if (null == arg)‘ instead of ‘if (arg == null)‘? I find the latter easier to read, and the problem the former solves in C doesn’t apply to C#.

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

Sidebar

Related Questions

I have a simple core-data entity that has a Boolean attribute called subscribedToNewsletter .
This answer shows that there is a built-in __name__ attribute of a function which
Is there a simple way to cache MySQL queries in PHP or failing that,
I'm building a really simple Core Data app in XCode 4. There's an entity
I'm familiar with core data but want to make a simple app that calculates
Is there anyway that I can check if an existing element has event handlers
Is there a simple way of getting a HTML textarea and an input type=text
Is there a simple way in .NET to quickly get the current protocol, host,
Is there any simple algorithm to determine the likeliness of 2 names representing the
Is there a simple method of parsing XML files in C#? If so, what?

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.