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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:54:27+00:00 2026-05-29T08:54:27+00:00

I found the following method in our code base today, and it strikes me

  • 0

I found the following method in our code base today, and it strikes me that it might not be thread-safe. I think IEnumerable could be a reference type, and could potentially be modified by another thread while this code is executing. Is that correct, or is this method thread-safe after all? If not, is it appropriate for this method to be static? It seems like making it an instance method wouldn’t change the possibility of the entities parameter being modified in another thread.

/// <summary>
/// Writes the Entity data in <paramref name="entities"/> to a CSV file located at <paramref name="path"/>.
/// </summary>
/// <typeparam name="T">The Entity Type.</typeparam>
/// <param name="entities">A List of Type LinqEntityBase.</param>
/// <param name="path">The location of the CSV file.</param>
internal static void LinqEntitiesToCsv<T>(IEnumerable<T> entities, string path) where T : LinqEntityBase
{
    var entityBuilder = new StringBuilder();
    List<KeyValuePair<string, int>> columnInfos = GetColumnInfos<T>();
    for (int i = 0; i < columnInfos.Count; i++)
    {
        string columnName = columnInfos[i].Key;
        entityBuilder.Append(columnName.Contains(',') ? columnName.WrapIn('\"') : columnName);
        entityBuilder.Append(i < columnInfos.Count - 1 ? "," : string.Empty);
    }

    entityBuilder.Append(Environment.NewLine);

    PropertyInfo[] propertyInfos = typeof (T).GetPropertiesFromCache().ToArray();
    foreach (T entity in entities)
    {
        for (int i = 0; i < propertyInfos.Length; i++)
        {
            var columnAttribute = Attribute
                                        .GetCustomAttribute(propertyInfos[i], typeof (ColumnAttribute))
                                    as ColumnAttribute;
            if (columnAttribute == null)
            {
                continue;
            }

            object value = propertyInfos[i].GetValue(entity, null);
            string valueString = (value != null) ? value.ToString() : string.Empty;

            entityBuilder.Append(valueString.Contains(',') ? valueString.WrapIn('\"') : valueString);
            entityBuilder.Append(i < columnInfos.Count - 1 ? "," : string.Empty);
        }

        entityBuilder.Append(Environment.NewLine);
    }

    FileHelper.TryWriteTextFile(path, entityBuilder.ToString());
}
  • 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-29T08:54:29+00:00Added an answer on May 29, 2026 at 8:54 am

    You are correct.

    The actual safety of the method depends on what you kind of IEnumerable you pass it; thread safety rarely exists in a vacuum.
    For example, all of the concurrent collections are completely thread-safe, even for concurrent writes and enumerations.

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

Sidebar

Related Questions

Today I found the following disturbingly ambiguous situation in our code base: class Base
I found the following code somewhere, but I am not understanding the code properly.
When reviewing our codebase, I found an inheritance structure that resembles the following pattern:
I'm converting an application to use Java 1.5 and have found the following method:
Found the following in an Oracle-based application that we're migrating (generalized) : SELECT Table1.Category1,
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
We have the following piece of code (idea for this code was found on
in the past we encountered various memory-leaks in our software. We found out that
I was looking at some code from a colleague and saw the following method:
I've found out the following article about A More Efficient Method for Paging Through

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.