The string returned by the function

If I write the following code:

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

My GetString () function will be called 3 times, then it can execute complex code 3 times. Is there an easier way:

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

to have only one code execution in GetString ()?

+4
source share
4 answers

You CAN make it more complex and have a bool IsChanged value, and if something in the object changes IsChanged to true and then rearranges the line, else will return the last line, but then you would need to add synchronization and it probably shouldn't have been would cost.

So long and short is that the second case is usually the best. It is simple and straightforward and efficient.

UPDATE . It is hard to say that the update script is here. So let's look at a few.

1). If you only update the line on demand from the application, and this line is created using some complex method, just use the new update method and then return the current line as a property. Then you can simply request the MyString property, and that 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 line without complex logic, you can make the application responsible for its creation and purpose:

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

But, as I said, this is only if the string representation is independent of the state of the object.

3). If it is based on the state of the object and changes when the state of the object changes, you can let it recreate the line 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 again, you probably want to sync this if it's multi-threaded.

BUT This is all IF , you want to cache the value so that it is not rebuilt every time as a responsibility of the class itself.

Truly, your easiest and best option is to simply use the second method, and if you use it several times in one method, just set a temporary variable and use it.

+2
source

As an alternative:

 var firstString = myObj.GetString(); var myString = string.Concat(firstString, firstString); 

string.Concat already manages the null case (treating it as an empty string).

+1
source

Another option is to cache the results after the first call to GetString. Subsequent calls will then use the cached copy. But the way you do this is great for most cases and simpler than implementing a cache.

0
source

Try something like this

 public static void Main(string[] args) { string s; if((s=getString())!=null) { Console.WriteLine(s); Console.ReadLine(); } } static string getString() { return "hello"; } 
0
source

All Articles