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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:53:51+00:00 2026-06-08T18:53:51+00:00

When specifying and creating numeric functions are there any C# idiomatic guidelines on when

  • 0

When specifying and creating numeric functions are there any C# idiomatic guidelines on when to return null and when to return NaN when both seem valid outputs.

The concrete example that has lead to this question is I am creating a percentage extension method for Enumerable collections. I have been thinking about what to return if there is an empty input. The function could return runningCount / totalCount which would return NaN on zero length inputs. However, I am not convinced that this sits well with the general feel of the language.

Are there any guidelines or language examples could inform the decision whether to retun NaN or null.

  • 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-08T18:53:52+00:00Added an answer on June 8, 2026 at 6:53 pm

    Idiomatic C# code throws exceptions when it is asked to do something that doesn’t make sense. Like calculating a percentage on empty input. That ought to be an InvalidOperationException.

    Returning magic values like null or NaN tends to just cause more trouble when the client code doesn’t check for it. It won’t, it also didn’t check that the input was empty so it won’t expect them. Null is nasty for the nondescript NullReferenceException it generates, giving no hint to what the real problem might be. NaN is nasty for just propagating undetectably, turning everything into NaN and ultimately generating nonsense results that you can’t trace back to the source.

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

Sidebar

Related Questions

Is there any way of specifying a URI (rather than a physical path) as
I know about specifying source locations using gdb. But I am wondering if there's
In the below code instead of specifying the tab name.. Is there anyway we
I'm creating a custom drawn window by specifying border style NONE and custom processing
I'm creating a fluid website design and am specifying min-width and max-width values, however,
I'm creating a DTS package. After specifying the source, destination, and tables, I get
I'm creating a CCLabelTTF without specifying a size. initWithString:fontName:fontSize: sets the dimensions to CGSizeZero
I'm creating a custom role provider and I set a Authorize attribute specifying a
I'm creating a notification for the notification tray. I'm specifying a click intent like
Is there any way to allow non-Administrator users to install, upgrade or uninstall an

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.