This is one of those "you can do it many ways" questions. Consider the following code:
protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference referece = new ScriptReference();
referece.Assembly = "FeyenoordEnabled";
referece.Name = "FeyenoordEnabled.PassTextBox.js";
return new ScriptReference[] { referece };
}
protected virtual IEnumerable<ScriptReference> GetScriptReferences()
{
ScriptReference referece = new ScriptReference();
referece.Assembly = "FeyenoordEnabled";
referece.Name = "FeyenoordEnabled.PassTextBox.js";
yield return referece;
}
I only need to return one item. The first piece of codes returns an array with a single item and the second one yields the item. What is better and why?
yield
is a pretty expensive keyword. You are telling the compiler to do a lot. If performance isn't an issue, go with the more elegant code. But if performance is an issue, stick with the array.
I can say from past experience that getting rid of this type of yield
usage has netted me some serious performance gains. But as always, profile and find the real bottlenecks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With