title: What are your favorite little used C# trick?
body: <blockquote>
  <p><strong>Possible Duplicate:</strong><br />
  <a href="https://stackoverflow.com/questions/9033/hidden-features-of-c">Hidden Features of C#?</a></p>
  </blockquote>
  
  <p>Hello Everyone,</p>
  <p>I'm just wondering what little C# tricks that programmers use in their day-to-day work.</p>
  <p>I have two - One, an overlooked, for me, language feature.  The other a solution to an often occuring need.</p>
  <h1>1 - Returning a default value (often from collections like ViewState or Session)</h1>
  <p>How often do you write code like this?</p>
  <pre><code>if(Session[&quot;VariableName&quot;] != null &amp;&amp; ....
  </code></pre>
  <p>or</p>
  <pre><code>if(String.IsNullOrEmpty(Session[&quot;VariableName&quot;]) ...
  </code></pre>
  <p>To retrieve the value.</p>
  <p>Well, Resharper slapped me on the hand the first time I tried that after I had installed it.  It said to use this instead:</p>
  <pre><code>var MyVariable = Session[&quot;VariableName&quot;] ?? string.empty
  </code></pre>
  <p>Soooo much easier.</p>
  <h1>2 - Counting the number of sub-strings in a string (Without looping in my code).</h1>
  <pre><code>string myString = &quot;Lorem ipsum dolor sit amet, &quot; +   
  &quot;consectetur adipisicing elit, sed do eiusmod &quot; + 
  &quot;tempor incididunt ut labore et dolore magna &quot; + 
  &quot;aliqua. Ut enim ad minim veniam, quis nostrud &quot; +
  &quot;exercitation ullamco laboris nisi ut aliquip &quot; +    
  &quot;ex ea commodo consequat. Duis aute irure &quot; + 
  &quot;dolor in reprehenderit in voluptate velit &quot; +    
  &quot;esse cillum dolore eu fugiat nulla pariatur. &quot; +    
  &quot;Excepteur sint occaecat cupidatat non proident, &quot; +    
  &quot;sunt in culpa qui officia deserunt mollit anim &quot; +    
  &quot;id est laborum.&quot;;
  
  string mySubString = &quot;dolor&quot;;
  int Count = (myString.Length - myString.Replace(mySubString, string.Empty).Length) / mySubString.Length;
  </code></pre>