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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:08:07+00:00 2026-06-15T10:08:07+00:00

While I’m working on parallelism of our framework, I’ve confronted a strange condition which

  • 0

While I’m working on parallelism of our framework, I’ve confronted a strange condition which I can’t imagine why! I simplified the situation to describe it easy.
consider this code:

foreach(var person in personList)
{
    if (person.Name == "Mehran")
        break;
}

which the personList is shared between multiple threads.

In what circumstances is it possible for person to be null and I get the NullReferenceException for person.Name?

As I know, the person is considered as a local variable here and if the we get into the foreach block, so we have iterated the personList successfully, so person should not be null in any circumstances or any parallel scenario.

Even if the personList is changed by another thread, or the referenced person is disposed, the person variable should have a value. Because no one has access to change where the person is referenced to.

Is it any scenario to explain the situation?

  • 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-15T10:08:08+00:00Added an answer on June 15, 2026 at 10:08 am

    As I know, the person is considered as a local variable here and if the we get into the foreach block, so we have iterated the personList successfully, so person should not be null in any circumstances or any parallel scenario.

    Just because you’re iterating over personList successfully doesn’t mean it doesn’t contain any null values. For example:

    List<Person> personList = new List<Person>();
    personList.Add(null);
    
    foreach (var person in personList)
    {
       // Here, person will be null
    }
    

    (Additionally, if anything is modifying the list, you’re generally in trouble – they’re not thread-safe in the face of writers – but I don’t think that needs to be part of the problem.)

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

Sidebar

Related Questions

While working in a project written by some one else ,there's a Function which
While playing with standard library i've found a strange difference between python2 and python3.
While refactoring my code base I found a piece of code which I'd like
While working on Hadoop Implementation in Pseudo-Distributed Operation, I found following exception of JAVA_HOME
while working with linq query i'm receiving error System.NullReferenceException: Object reference not set to
While working with annotations I stumbled accross the following piece of code (it's the
While I can normally do commits and also push to other remote repositories (eg.
While working on an auditing project I came across different problems regarding software and
While working on the Clojure Koans, I had to calculate the factorial of a
While working with Valgrind tool, i need to log the details produced by valgrind

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.