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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:21:28+00:00 2026-05-25T02:21:28+00:00

If I write the following code : if(string.IsNullOrEmpy(myObj.GetString())) { var myString = myObj.GetString() +

  • 0

If I write the following code :

if(string.IsNullOrEmpy(myObj.GetString()))
{
  var myString  = myObj.GetString() + myObj.GetString();
}

My function GetString() will be called 3 times, then it can execute some complex code 3times. Is there a simpler way than :

var firstString = myObj.GetString();
if(!string.IsNullOrEmpy(firstString))
{
  var myString  = firstString  + firstString;
}

to have only one execution of the code in GetString() ?

  • 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-25T02:21:29+00:00Added an answer on May 25, 2026 at 2:21 am

    You COULD make it more complex and have an IsChanged bool value, and if anything in the object changes set IsChanged to true and then rebuild the string, else return the last string built, but then you’d need to add synchronization and it probably wouldn’t be worth the cost.

    So, the long and the short is that the 2nd case is generally the best case. It’s simple and straightforward and efficient.

    UPDATE: It’s hard to tell what the update scenario is here. So let’s look at several.

    1). If you are only updating the string on demand from the application, and that string is created using some complex method, just have a new update method and then return the current string as a property. Then, you can just query the MyString property and it will be a simple return.

    public class SomeClass
    {
        public string MyString { get; private set; }
    
        public void UpdateString(...)
        {
           // DO YOUR COMPLEX LOGIC
    
           MyString = ... new value ...
        }
    }
    

    2). Or, if it’s just a simple string with no complex logic, you could have the application responsible for creating and assigning it:

    public class SomeClass
    {
        public string MyString { get; set; }
    }
    
    ...
    
    myObj.MyString = SomeComplexLogicToBuildString();
    

    But as I said, that’s only if the string representation is independent from the state of the object.

    3). If it’s based on the state of the object and changes when the object state changes, you could let the string be re-created whenever something changes:

    public class SomeClass
    {
        private bool _hasChanged = true;
        private string _previousString = null;
    
    
        public string MyString
        {
            get 
            {
                if (_hasChanged)
                {
                    _hasChanged = false;
                    _previousString = .... your complex string building logic ....
                } 
    
                return _previousString;
            }
        }
    
        public int OtherProperties
        {
            get { return _otherField; }
            set { _otherField = value; _hasChanged = true; }
        }
    

    But once again, you’d probably want to synchronize this if it’s multi-threaded use.

    BUT This is all IF you want to cache the value so it’s not rebuilt each time as a responsibility of the class itself.

    Truly, your simplest and best bet is just to use your second method, and if you are using it several times in one method just set to a temporary variable and use that.

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

Sidebar

Related Questions

How can I write the following code quickly in emacs? \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B}
I have the following code. var d3_ad = 1; function displayD3Ad(){ var query_string =
I want to create a Regex in c#,I write the following code: string temp
The following code is an attempt to write a variadic function that acts like
Consider the following code: var myDict = new Dictionary<string, int>(); myDict.Add(Key1, 1); myDict.Add(Key2, 2);
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I needed some simple string encryption, so I wrote the following code (with a
I write the following code all the time to handle when the enter key
If I write the following code: session_start(); $_SESSION['user_id']='daniel'; the variable stays fine as long

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.