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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:15:53+00:00 2026-05-30T19:15:53+00:00

Can anyone explain why the code below, when run, will occasionally result in some

  • 0

Can anyone explain why the code below, when run, will occasionally result in some “NULL!” strings being written to the Console window?

(That’s the TL;DR question, read on for further details)

This doesn’t happen every time and you may need to run this code a few times, but soon enough, you’ll see a string of “NULL!” being output to the Console window instead of a number.

Essentially, it’s initializing (using the shorthand collection initializer syntax) a generic collection of user-defined types (List<Thing>) that contains a single property which is a reference to another user-defined type (Numb). Numb is pre-defined in a collection of it’s own (nnn) and objects from that collection are created using the shorthand object initializer syntax from within the collection initializer using a lambda to select a random object based upon it’s sole num property.

The nnn.Where(n => n.num==rnd.Next(1,3)).FirstOrDefault() should never return a NULL object (i.e. the default part of FirstOrDefault()) as the random number should only ever select either a 1 or a 2, both of which exist within the nnn collection.

My suspicions tell me this is something to do with using a call (specifically multiple calls) to rnd.Next() within an object/collection initializer scenario.

The code (run as a console application):

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();

            var nnn = new List<Numb> {
                new Numb() {
                    num = 1
                },
                new Numb() {
                    num = 2
                }
            };

            new List<Thing> {
                new Thing() {
                    numb = nnn.Where(n => n.num==rnd.Next(1,3)).FirstOrDefault()
                },
                new Thing() {
                    numb = nnn.Where(n => n.num==rnd.Next(1,3)).FirstOrDefault()
                },
                new Thing() {
                    numb = nnn.Where(n => n.num==rnd.Next(1,3)).FirstOrDefault()
                },
                new Thing() {
                    numb = nnn.Where(n => n.num==rnd.Next(1,3)).FirstOrDefault()
                }
            }.ForEach(t => Console.WriteLine(t.numb!=null ? t.numb.num.ToString() : "NULL!"));

            Console.ReadLine();
        }
    }

    public class Thing
    {
        public Numb numb { get; set; }
    }

    public class Numb
    {
        public int num {get; set; }
    }
}
  • 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-30T19:15:54+00:00Added an answer on May 30, 2026 at 7:15 pm

    You’re generating a different random number on each test of the predicate. So just looking at one part:

    new Thing() {
        numb = nnn.Where(n => n.num==rnd.Next(1,3)).FirstOrDefault()
    }
    

    That’s going to:

    • Start iterating, get the first Numb – let’s call that n1
    • Generate a random number, 1 or 2 – let’s call that x
    • Check whether n1.num == x, and yield it if so
    • Assuming the previous step didn’t match, it will…
      • Continue iterating, getting the second Number – let’s call that n2
      • Generate another random number, 1 or 2 – let’s call that y
      • Check whether n2.num == y, and yield it if so
      • Assuming the previous step didn’t match, it will return null

    In other words, you’re querying against a moving target. You want to generate one random number, then test against that for all of the values. (Then repeat for each member of the collection initializer.)

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

Sidebar

Related Questions

Can anyone explain the below code can any one give some real time explanation
Can anyone explain to me why GetInterfaces() in the below code returns an interface
Can anyone explain to me why this code below does not work? #include opencv/cv.h
can anyone explain why the code below fails? type TIDEThemeObserverFunc = reference to procedure(foo:
I'm using the code below and receiving the FileNotFound exception. Can anyone explain why
Can anyone help explain why the code below fails to retrieve the html Dim
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain why this code gives the error: error C2039: 'RT' : is
Can anyone explain why this code with the given routes returns the first route?
Can anyone explain the following PHP Code ? function get_param($param_name, $param_type = 0) {

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.