String or StringBuilder return values?

前端 未结 5 1766
生来不讨喜
生来不讨喜 2021-02-12 19:06

If I am building a string using a StringBuilder object in a method, would it make sense to:

Return the StringBuilder object, and let the calling code call ToString()?

5条回答
  •  再見小時候
    2021-02-12 19:18

    I think it depends what you are doing with the string once it leaves the method. If you are going to continue appending to it then you might want to consider returning a stringbuilder for greater efficiency. If you are always going to call .ToString() on it then you should do that inside the method for better encapsulation.

提交回复
热议问题