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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:09:37+00:00 2026-05-15T12:09:37+00:00

Does a StringBuilder variable need to be initialized to lets say string.empty in case

  • 0

Does a StringBuilder variable need to be initialized to lets say string.empty in case you don’t end up appending anything in the end and need to return something?

    public static string ToSemiColonList<T>(this IEnumerable<T> list, Func<T, string> func)
    {
        StringBuilder sb = new StringBuilder();

        foreach (T item in list)
        {
            if (sb.Length > 0)
                sb.Append(";");

            string elem = func(item);
            sb.Append(elem);
        }

        return sb.ToString();
    }

In this case it may never enter the foreach. So my guess is since every local variable in a method scope is set to no value in C#, to set it to an empty string since we need to give something back if this foreach is not hit.

Maybe it’s better to check the stringbuilder length then if zero, return string.empty or am I going overboard (doing the same work twice) and it’s fine like I have it?

  • 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-15T12:09:38+00:00Added an answer on May 15, 2026 at 12:09 pm

    Default initialization of a StringBuilder object gets set to string.Empty.

    The default capacity is 16 – see here for a similar (but different) question on SO.

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

Sidebar

Related Questions

How the StringBuilder class is implemented? Does it internally create new string objects each
Why does StringBuffer/StringBuilder does not override the equals() , hashcode() methods from object? Please
Does anyone know why UsernameExists wont return True. I must have my syntax messed
My C# (.NET 2.0) application has a StringBuilder variable with a capacity of 2.5MB.
How does the following code work? public void SomeMethod() { StringBuilder sb = new
Error 3 Running transformation: System.ArgumentException: Format of the initialization string does not conform to
How does one effectively dispose a StringBuilder object? If an user generates multiple reports
I was doing tests with the Java StringBuilder, especially the replace(int, int, String) function
Is there a way to append two string builders? And if so - does
Possible Duplicate: Difference between string and StringBuilder in c# What’s the difference between System.String

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.