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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:57:39+00:00 2026-06-16T13:57:39+00:00

Class example: public class SomeType { private int type; // some code… public override

  • 0

Class example:

public class SomeType
{
    private int type;

    // some code...

    public override string ToString ()
    {
       if (type == 1) return "One";
       if (type == 2) return "Two";
    }
}

Now imagine application calls thousand times ToString() method in a second.

My question is: when I use inline created string in code like something = myClass.ToString() is in every call created a new string or compiler optimize it somehow? (because strings are immutable it could be returned only referense to a static string).

And if not, should I make static private string fields and return them in ToString method for performance reasons?

Ofcourse I will test it using Stopwatch, but I need an expert answer anyway.

  • 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-06-16T13:57:41+00:00Added an answer on June 16, 2026 at 1:57 pm

    You’re using string literals – which means you’re returning a reference to the same string each time. This is guaranteed by the language specification. From section 2.4.4.5 of the C# 5 specification:

    When two or more string literals that are equivalent according to the string equality operator (§7.10.7) appear in the same program, these string literals refer to the same string instance.

    So as a simpler example:

    string x = "One";
    string y = "One";
    Console.WriteLine(object.ReferenceEquals(x, y)); // Prints True
    

    In your code, the ToString() method will still be called – but it won’t create a new string object each time. You might consider using a switch statement instead of all those if statements, by the way.

    Note that even if it did create a new string each time, creating thousands of strings per second won’t make a modern CPU break into a sweat. Both the allocator and garbage collector are pretty efficient, and modern computers can do an awful lot of work in a second.

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

Sidebar

Related Questions

struct GPattern() { int gid; .... } class Example() { public: void run(string _filename,
Take this class as example: public class Category : PersistentObject<int> { public virtual string
Consider this simple example - public class Person { private String name; private Date
I have this code: import java.util.Scanner; public class Example { public static void main(String[]
Take the following class as an example: class Sometype { int someValue; public Sometype(int
Example class code: <?php class example { public function forExample() { return true; }
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;
Say suppose I have the following Java code. public class Example { public static
Example: public class TestClass { public static void main(String[] args) { TestClass t =
In the following class: public class Example<T> where T : IComparable { private T

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.