<p>This is a fairly trivial matter, but I'm curious to hear people's opinions on it.</p>

<p>If I have a Dictionary which I'm access through properties, which of these formats would you prefer for the property?</p>

<pre><code>/// &lt;summary&gt;
/// This class's FirstProperty property
/// &lt;/summary&gt;
[DefaultValue("myValue")]
public string FirstProperty {
    get {
        return Dictionary["myKey"];
    }
    set {
        Dictionary["myKey"] = value;
    }
</code></pre>

<p>This is probably the typical way of doing it. It's fairly efficient, easy to understand, etc. The only disadvantage is with a longer or more complex key it would be possible to misspell it or change only one instance or something, leading me to this:</p>

<pre><code>/// &lt;summary&gt;
/// This class's SecondProperty property
/// &lt;/summary&gt;
[DefaultValue("myValue")]
private const string DICT_MYKEY = "myKey"
public string SecondProperty {
    get {
        return Dictionary[DICT_MYKEY];
    }
    set {
        Dictionary[DICT_MYKEY] = value;
    }
</code></pre>

<p>Which is marginally more complicated, but seems to offer additional safety, and is closer to what I would think of as the "Code Complete" solution. The downside is that when you also have a ///  block and a [DefaultValue()] block above the property already, it starts getting a bit crowded up there.</p>

<p>So which do you like better, and why? Does anybody have any better ideas?</p>

[{'answer_id': 44106, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 3, 'selected': True, 'text': "<p>I like the second one purely because any avoidance of magic strings/numbers in code is a good thing.  IMO if you need to reference a number or string literal in code more than once, it should be a constant.  In most cases even if it's only used once it should be in a constant </p>\n"}, {'answer_id': 44121, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with @Glenn for a purely nit-picky point of view.   The answer is whatever works for you.  All this code takes place in 10 lines (if you include the omitted last curly brace).  Nobody is going to get lost and the chance of mistyping is pretty slim (not impossible but very slim).  On the other hand, if you used the key somewhere else, then DEFINATELY go with the constant.</p>\n\n<p>Personally, I would go off on you about your curly brace style. :)  Just kidding!  It really is a matter of style.</p>\n'}, {'answer_id': 44126, 'author': 'roomaroo', 'author_id': 3464, 'author_profile': 'https://Stackoverflow.com/users/3464', 'pm_score': 0, 'selected': False, 'text': '<p>This isn\'t answering your question, but I don\'t think "DefaultValue" means what you think it means. It doesn\'t set a default value for your property.</p>\n\n<p>See <a href="http://support.microsoft.com/kb/311339" rel="nofollow noreferrer">MSDN</a> and <a href="https://stackoverflow.com/questions/43738/defaultvalue-for-systemdrawingsystemcolors#44073">this question</a> for more details.</p>\n'}, {'answer_id': 44130, 'author': 'Robert Durgin', 'author_id': 3132, 'author_profile': 'https://Stackoverflow.com/users/3132', 'pm_score': 0, 'selected': False, 'text': '<p>A lot of people would probably argue that the second option is "correct", because any value used more than once should be refactored into a constant. I would most likely use the first option. You have already gotten close to the "Code Complete" solution by encapsulating the dictionary entry in a strong typed property. This reduces the chance of screwing up retrieving the wrong Dictionary entry in your implementation. \nThere are only 2 places where you could mess up typing "myKey", in the getter and setter, and this would be very easy to spot. </p>\n\n<p>The second option would just get too messy. </p>\n'}, {'answer_id': 44134, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': '<p>You could match the property names up to the keys and use reflection to get the name for the lookup.</p>\n\n<pre><code>public string FirstProperty {\nget {\n    return Dictionary[PropertyName()];\n}\nset {\n    Dictionary[PropertyName()] = value;\n}\n\nprivate string PropertyName()\n{\n    return new StackFrame(1).GetMethod().Name.Substring(4);\n}\n</code></pre>\n\n<p>This has the added benefit of making all your property implementation identical, so you could set them up in visual studio as code snippets if you want.</p>\n'}, {'answer_id': 44140, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 0, 'selected': False, 'text': "<p>When you only use a magic string in one context, like you do, I think it's alright.<br>\nBut if you ever need to use the key in another part of the class, go <code>const</code>.</p>\n"}, {'answer_id': 44233, 'author': 'hometoast', 'author_id': 2009, 'author_profile': 'https://Stackoverflow.com/users/2009', 'pm_score': 0, 'selected': False, 'text': '<p>@Joel you don\'t want to count on StackFrame.  <a href="http://discuss.joelonsoftware.com/default.asp?dotnet.12.511358.10" rel="nofollow noreferrer">In-lining</a> can ruin your day when you least expect it.</p>\n\n<p>But to the question: Either way doesn\'t really matter a whole lot.</p>\n'}]
['https://Stackoverflow.com/questions/44100', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1512/']