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

  • Home
  • SEARCH
  • 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 1093423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:49:31+00:00 2026-05-16T23:49:31+00:00

I am working with a StringWriter which I am passing to a method to

  • 0

I am working with a StringWriter which I am passing to a method to write values in a foreach loop. I believe this is causing the generation of two warnings:

CA2000 : Microsoft.Reliability : In method ‘ToCsvService.ToCsv()’, object ‘sw’ is not disposed along all exception paths. Call System.IDisposable.Dispose on object ‘sw’ before all references to it are out of scope.

and

CA2202 : Microsoft.Usage : Object ‘sw’ can be disposed more than once in method ‘ToCsvService.ToCsv()’. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

public string ToCsv()
{
    IEnumerable<string> props = GetProperties();
    StringWriter sw = new StringWriter(); // first warning here
    sw.WriteLine(GetHeadings(props));
    WriteValues(props, sw);

    sw.Close();
    string returnCsv = sw.ToString();
    sw.Dispose(); // second warning here

    return returnCsv;
}

I’ve left out GetProperties() from the list of methods called as it didn’t seem pertinent.

private string GetHeadings(IEnumerable<string> props)
{
    string headings = String.Join(",",
        props.Select(prop =>
            _headings.ContainsKey(prop) ? _headings[prop] : prop));

    return headings;
}

private void WriteValues(IEnumerable<string> props, StringWriter sw)
{
    foreach (object obj in _collection)
    {
        var x = obj.GetType().GetProperties()
            .Where(pi => props.Contains(pi.Name))
            .Select(pi =>
                _format.ContainsKey(pi.Name)
                ? String.Format("{0:" + _format[pi.Name] + "}",
                                pi.GetGetMethod().Invoke(obj, null))
                : pi.GetGetMethod().Invoke(obj, null).ToString());

        string values = String.Join<string>(",", x);

        sw.WriteLine(values);
    }
}

Why are these warnings being generated?

  • 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-16T23:49:32+00:00Added an answer on May 16, 2026 at 11:49 pm

    Your code allows for the possibility that a thrown exception causes execution to skip over the statement that closes your StringWriter. You want to ensure that, before an exception causes execution to leave ToCSV, you close sw.

    The easiest way to handle this is with a using block. An object constructed within a using clause is guaranteed to be disposed before the scope of the block is exited:

    public string ToCsv()
    {
        IEnumerable<string> props = GetProperties();
        using (StringWriter sw = new StringWriter())
        {
            sw.WriteLine(GetHeadings(props));
            WriteValues(props, sw);
            return sw.ToString();
        }
    }
    

    Note that you don’t need to call both Close and Dispose on the StringWriter. Just Dispose is enough.

    In general, you’ll want to wrap a using block around the creation and use of all objects that implement IDisposable (as StringWriter does). That’ll ensure that, no matter what exceptions get thrown, the object is always disposed of properly.

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

Sidebar

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.