String vs. StringBuilder

后端 未结 24 1260
眼角桃花
眼角桃花 2020-11-22 06:22

I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference

24条回答
  •  感情败类
    2020-11-22 06:56

    Yes, StringBuilder gives better performance while performing repeated operation over a string. It is because all the changes are made to a single instance so it can save a lot of time instead of creating a new instance like String.

    String Vs Stringbuilder

    • String

      1. under System namespace
      2. immutable (read-only) instance
      3. performance degrades when continuous change of value occures
      4. thread safe
    • StringBuilder (mutable string)

      1. under System.Text namespace
      2. mutable instance
      3. shows better performance since new changes are made to existing instance

    Strongly recommend dotnet mob article : String Vs StringBuilder in C#.

    Related Stack Overflow question: Mutability of string when string doesn't change in C#?.

提交回复
热议问题