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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:29:34+00:00 2026-05-29T07:29:34+00:00

How many String objects will be created when using a plus sign in the

  • 0

How many String objects will be created when using a plus sign in the below code?

String result = "1" + "2" + "3" + "4";

If it was as below, I would have said three String objects: “1”, “2”, “12”.

String result = "1" + "2";

I also know that String objects are cached in the String Intern Pool/Table for performance improvement, but that’s not the question.

  • 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-29T07:29:35+00:00Added an answer on May 29, 2026 at 7:29 am

    Surprisingly, it depends.

    If you do this in a method:

    void Foo() {
        String one = "1";
        String two = "2";
        String result = one + two + "34";
        Console.Out.WriteLine(result);
    }
    

    then the compiler seems to emit the code using String.Concat as @Joachim answered (+1 to him btw).

    If you define them as constants, e.g.:

    const String one = "1";
    const String two = "2";
    const String result = one + two + "34";
    

    or as literals, as in the original question:

    String result = "1" + "2" + "3" + "4";
    

    then the compiler will optimize away those + signs. It’s equivalent to:

    const String result = "1234";
    

    Furthermore, the compiler will remove extraneous constant expressions, and only emit them if they are used or exposed. For instance, this program:

    const String one = "1";
    const String two = "1";
    const String result = one + two + "34";
    
    public static void main(string[] args) {
        Console.Out.WriteLine(result);
    }
    

    Only generates one string- the constant result (equal to “1234”). one and two do not show up in the resulting IL.

    Keep in mind that there may be further optimizations at runtime. I’m just going by what IL is produced.

    Finally, as regards interning, constants and literals are interned, but the value which is interned is the resulting constant value in the IL, not the literal. This means that you might get even fewer string objects than you expect, since multiple identically-defined constants or literals will actually be the same object! This is illustrated by the following:

    public class Program
    {
        private const String one = "1";
        private const String two = "2";
        private const String RESULT = one + two + "34";
    
        static String MakeIt()
        {
            return "1" + "2" + "3" + "4";
        }   
    
        static void Main(string[] args)
        {
            string result = "1" + "2" + "34";
    
            // Prints "True"
            Console.Out.WriteLine(Object.ReferenceEquals(result, MakeIt()));
    
            // Prints "True" also
            Console.Out.WriteLine(Object.ReferenceEquals(result, RESULT));
            Console.ReadKey();
        }
    }
    

    In the case where Strings are concatenated in a loop (or otherwise dynamically), you end up with one extra string per concatenation. For instance, the following creates 12 string instances: 2 constants + 10 iterations, each resulting in a new String instance:

    public class Program
    {
        static void Main(string[] args)
        {
            string result = "";
            for (int i = 0; i < 10; i++)
                result += "a";
            Console.ReadKey();
        }
    }
    

    But (also surprisingly), multiple consecutive concatenations are combined by the compiler into a single multi-string concatenation. For example, this program also only produces 12 string instances! This is because “Even if you use several + operators in one statement, the string content is copied only once.“

    public class Program
    {
        static void Main(string[] args)
        {
            string result = "";
            for (int i = 0; i < 10; i++)
                result += "a" + result;
            Console.ReadKey();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application builds many objects in memory based on filenames (among other string based
I've got a class with many string arrays. I'd like to have one generic
I have an object which I have created from an XML document using visual
I am currently storing many different types of objects in the ASP.NET HttpRuntime.Cache and
There are many ways of converting a String to an Integer object. Which is
Many people string together find and sed, or perl, or any number of other
I need to replace many different sub-string in a string in the most efficient
I tried decoding this base64 string so many times but every time i get
I tried many things but I always get cannot convert string to membershipuser from
I was really discouraged by java's string encoding. There are many auto conversions in

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.