none
How to fix CA1305 message when use StringBuilder.AppendLine

    Question

  • Hi there,

    I'm trying to understood, but I can't find the solution.

    I use StringBuilder.AppendLine() and it always shows me CA1305 warning. How can I eliminate it?

    Example:

    var message = new StringBuilder("My error:");

    message.AppendLine("error line 1");

    message.AppendLine("error line 2");

    message.AppendLine("etc...");

    I would like to use InvariantCulture....

    Thursday, April 13, 2017 3:09 PM

Answers

  • I would go so far as to say don't use the StringBuilder at all. Unless you need to piecemeal a string together with disparate data then combining strings doesn't need StringBuilder.

    var messages = new List<string>();
    
    //Just to collect the data - assume this is actually coming from somewhere else
    messages.Add("My error");
    messages.Add("error line 1");
    messages.Add("error line 2");
    messages.Add("etc...");
    
    //All done so combine them
    var message = String.Join(Environment.NewLine, messages);

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by Tigyi János Wednesday, April 19, 2017 7:56 AM
    Friday, April 14, 2017 2:06 PM
    Moderator

All replies

  • Hello,

     Take a look and read the How to Fix section;

     https://msdn.microsoft.com/en-us/library/ms182190.aspx

    Hope this helps :)

    Thursday, April 13, 2017 6:09 PM
  • The StringBuilder.AppendLine method has no overload that takes a CultureInfo or IFormatProvider but you could suppress the warning by right-click on it in the error list in Visual Studio and choose Suppress->In Source.      


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, April 13, 2017 7:23 PM
  • I would go so far as to say don't use the StringBuilder at all. Unless you need to piecemeal a string together with disparate data then combining strings doesn't need StringBuilder.

    var messages = new List<string>();
    
    //Just to collect the data - assume this is actually coming from somewhere else
    messages.Add("My error");
    messages.Add("error line 1");
    messages.Add("error line 2");
    messages.Add("etc...");
    
    //All done so combine them
    var message = String.Join(Environment.NewLine, messages);

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by Tigyi János Wednesday, April 19, 2017 7:56 AM
    Friday, April 14, 2017 2:06 PM
    Moderator
  • It doesn't kills the warning, but I can use string.format with InvariantCulture, that solves the warning...
    Thursday, April 20, 2017 7:55 AM