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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:46:47+00:00 2026-05-16T17:46:47+00:00

Can someone explain what is going on here? Howcome both these things are true?

  • 0

Can someone explain what is going on here? Howcome both these things are true?

[TestMethod]
public void WhatIsGoingOnHere()
{
    List<int?> list = new List<int?> { 1, 2, 3, null, 5, 6 };
    Assert.AreEqual(17, list.Sum());

    int? singleSum = 1 + 2 + 3 + null + 5 + 6;

    Assert.IsNull(singleSum);
}

Specifically, why does the Sum() method not return ‘null’? Or the singleSum not equal 17?

  • 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-16T17:46:47+00:00Added an answer on May 16, 2026 at 5:46 pm

    What you are seeing is the difference between using Enumerable.Sum and actually adding the values yourself.

    The important thing here is the null is not zero. At first glance you would think that singleSum should equal 17 but that would mean that we would have to assign different semantics to null based on the data type of the reference. The fact that this is an int? makes no difference – null is null and should never be semantically equal with the numeric constant 0.

    The implementation of Enumerable.Sum is designed to skip over any value that is null in the sequence so that is why you are seeing the different behavior between the two tests. However the second test rightly returns null as the compiler is smart enough to know that adding anything to null yields null.

    Here is the implementation of Enumerable.Sum that accepts a parameter of int?:

    public static int? Sum(this IEnumerable<int?> source)
    {
        if (source == null)
        {
            throw Error.ArgumentNull("source");
        }
        int num = 0;
        foreach (int? nullable in source)
        {
            // As you can see here it is explicitly designed to
            // skip over any null values
            if (nullable.HasValue)
            {
                num += nullable.GetValueOrDefault();
            }
        }
        return new int?(num);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone visually explain what's going on here please. var stack = []; function
Can someone explain what's going on here, and how to fix it? I'm using
Can someone please explain what is going on here with the Django Tutorial Part
http://www.mapleboutique.com/_blog/maple_blog can someone explain what's going on here? according to this link: http://austinmatzko.com/2007/07/25/internet-explorer-7-float-bug/ –I
Can someone explain in English what is going on here? std::vector<Cat*> cats; //I get
Can someone explain me what exactly is going on here? I am not fully
Can someone explain the nuts and bolts of what's really going on under the
I've been going through the learn.knockout.js tutorials and been experimenting. Can someone explain why
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in

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.