Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 209

Are two dictionary lookups triggered when assigning and returning a dictionary value on a single line?

$
0
0

Consider this C# example:

public sealed class TestClass {  private Dictionary<string,int> myDictionary = [];  public int TestMethod() => myDictionary["foo"] = 3;}

Internally, when TestMethod() is called, will there be two dictionary lookups (one for setting the foo key and one for returning that key's value from the dictionary)? Or is the compiler smart enough to only do a single lookup for setting the foo key, and simply return the value 3?

I'm just wondering if I can shorten this:

public sealed class TestClass {  private Dictionary<string,int> myDictionary = [];  public int TestMethod() {    var val = 3;    myDictionary["foo"] = val;    return val;  }}

Viewing all articles
Browse latest Browse all 209

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>