title: Generic methods returning simple types
body: <p>Ok,
  I've got a bit of code that I don't like very much. Given that I didn't write it and merely inherited it, it occurs to me that I could clean it up a little. Basically, we have an object for settings that is persisted with NHibernate. I won't bore you with the implementation details except to say that it contains a dictionary that has the actual values we're keeping in the database.</p>
  
  <p>Our typical property implementation looks like this:</p>
  
  <pre><code>public string MyDumbProperty
  {
  get { return GetStringValue("MyDumbProperty", string.Empty); }
  set { SetValue("MyDumbProperty", value);}
  }
  </code></pre>
  
  <p>Now, it's probably obvious from the implementation above that we have separate Getxxxx methods for each of the types we return (strings, ints, longs, floats, etc.), with the second parameter being a default value. What I want is to be able to do something like the following:</p>
  
  <pre><code>public string MyDumbProperty
  {
  get { return GetValue("MyDumbProperty", string.Empty); }
  set { SetValue("MyDumbProperty", value);}
  }
  </code></pre>
  
  <p>I have a couple of reasons for wanting to do this. The big one is that I have a personal dislike of methods named based on what types of parameters they take. The second is that I'd like to make a resharper template for this crap. Then I could just fill in the name of the property and the type and be done (presuming that resharper could give a reasonable default value for the type).</p>
  
  <p>I had thought of making a generic method GetValue that returned a T, but I'm not sure how I need to set up the generic constraints so this is doable. The types being returned from this will all be basic types (strings, ints, etc). There are a couple that return arrays, but I could always do something different on those. This isn't a high priority issue, by any means, but it annoys me every time I see it. I just think there should be a better way to do this.</p>
  
  <p>I suppose a first step would be to just rename the various methods. Since they vary on what type is being passed for the default in the second parameter, I could at least coerce them to the same name. But I could I do better than that? It would be great if I could get rid of the multiple bits of repetitive code in each method that are only different in terms of the datatype being used (TryParse in all the numeric ones).</p>