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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:27:09+00:00 2026-05-25T00:27:09+00:00

Question: Is there a significant performance hit when assigning a variable to itself? Although

  • 0

Question: Is there a significant performance hit when assigning a variable to itself?

Although the code can be easily changed to avoid assigning a variable to itself or to use flags to detect how something should be assigned .. I like the use of a null coalescing operator to make cleaner initialization code. Consider the following code:

   public class MyObject
   {
      public string MyString { get; set; }
   }

   public class Worker
   {
      Dictionary<int, string> m_cache = new Dictionary<string, string>();

      public void Assign(ref MyObject obj)
      {
         string tmp = null;
         if (some condition)
         {
            //can't pass in MyObject.MyString to out param, so we need to use tmp
            m_cache.TryGetValue("SomeKey", out tmp); //assumption: key is guaranteed to exist if condition is met
         }    
         else
         {
             obj.MyString = MagicFinder(); //returns the string we are after
         }
         //finally, assign MyString property
         obj.MyString = tmp ?? obj.MyString; 

         //is there a significant performance hit here that is worth changing 
         //it to the following?

         if (!string.IsEmptyOrNull(tmp))
         {
             obj.MyString = tmp;
         }
      }
   }
  • 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-25T00:27:09+00:00Added an answer on May 25, 2026 at 12:27 am

    Your question seems to boil down to this section:

    //finally, assign MyString property
    obj.MyString = tmp ?? obj.MyString; 
    

    To:

    //is there a significant performance hit here that is worth changing 
    //it to the following?
    if (!string.IsNullOrEmpty(tmp))
    {
            obj.MyString = tmp;
    }
    

    Realize, however, that the two versions are not the same. In the first case (using the null coalescing operation), obj.MyString can get assigned to string.Empty. The second case prevents this explicitly, since it’s checking against string.IsNullOrEmpty, which will precent the assignment in the case of a non-null, empty string.

    Since the behavior is different, I would say this is not an optimization, but rather a behavioral change. Whether this is appropriate depends on the behavior this method is specified to demonstrate. How should empty strings be handled? That should be the determining factor here.

    That being said, if you were to do an explicit null check, the performance would be near identical – I would suggest going for maintainability and readability here – to me, the first version is simpler, as it’s short and clear.


    Also, looking at your code, I think it would be far simpler to put the assignment into the statement. This would make the code far more maintainable (and more efficient, to the delight of your boss…):

      public void Assign(ref MyObject obj)
      {         
         if (some condition)
         {
            string tmp = null;
            //can't pass in MyObject.MyString to out param, so we need to use tmp
            m_cache.TryGetValue("SomeKey", out tmp); //assumption: key is guaranteed to exist if condition is met
            obj.MyString = tmp;
         }    
         else
         {
             obj.MyString = MagicFinder(); //returns the string we are after
         }
      }
    

    Given the assumption in the code (that the key will always exist in cache if your condition is true), you can even simplify this down to:

      public void Assign(MyObject obj) // No reason to pass by ref...
      {         
         obj.MyString = someCondition ? m_cache["SomeKey"] : MagicFinder();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there significant performance differences (at runtime and/or compile time) between constructing and assigning
We have the question is there a performance difference between i++ and ++i in
Or to reformulate the question: is there a performance penalty in using unsigned values?
I have a very theoretical question: Is there a way to ban the use
I was reading Holgerwa's question and I have a question. Is there any significant
Question Is there a way to have a method that will always run anytime
Question: Is there an easy way (library function) to perform a bitwise AND or
Question: Is there any reason Autocomplete=off on a ASP:Textbox would not be working in
Question Is there a way to define a method only once in C# (in
Possible duplicate question: Is there a way to indefinitely pause a thread? In my

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.