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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:19:51+00:00 2026-05-23T15:19:51+00:00

Description of ConcurrentBag on MSDN is not clear: Bags are useful for storing objects

  • 0

Description of ConcurrentBag on MSDN is not clear:

Bags are useful for storing objects when ordering doesn’t matter, and unlike sets, bags support duplicates. ConcurrentBag is a thread-safe bag implementation, optimized for scenarios where the same thread will be both producing and consuming data stored in the bag.

My question is it thread safe and if this is a good practice to use ConcurrentBag in Parallel.ForEach.

For Instance:

    private List<XYZ> MyMethod(List<MyData> myData)
    {
        var data = new ConcurrentBag<XYZ>();

        Parallel.ForEach(myData, item =>
                        {
                            // Some data manipulation

                            data.Add(new XYZ(/* constructor parameters */);
                        });

        return data.ToList();
    }

This way I don’t have to use synchronization locking in Parallel.ForEach using regular List.

Thanks a lot.

  • 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-23T15:19:51+00:00Added an answer on May 23, 2026 at 3:19 pm

    That looks fine to me. The way you’re using it is thread-safe.

    If you could return an IEnumerable<XYZ>, it could be made more efficient by not copying to a List<T> when you’re done.

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

Sidebar

Related Questions

Description for Assert.Equals() from the MSDN Documentation: Do not use this method. That's it,
Description: If we use java objects jruby get permgen too: System.out.println(Initialazing..); //Spring applicaton context
Description This is not a real world example! Please don't suggest using decimal or
Description: The application attempted to perform an operation not allowed by the security policy.
Description of LEN() function on MSDN : Returns the number of characters of the
Description of the permission: Allows applications to open network sockets. It doesn't seem like
OVERVIEW/DESCRIPTION Simple: Polymorphic removal of objects of runtime types derived from TEntity added to
Description: Obtain output from an executable Note: Will not compile, due to fgets() declaration
description_id description -------------- ------------- 1 Ampicillin Oral 1 Ofloxacin Oral 1 Sulfamoxole+Trimethoprim Oral 2
Description : I am creating customized title bar. The codes for customization is as

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.