<p>How would you refactor these two classes to abstract out the similarities? An abstract class? Simple inheritance? What would the refactored class(es) look like?</p>

<pre><code>public class LanguageCode
{
    /// &lt;summary&gt;
    /// Get the lowercase two-character ISO 639-1 language code.
    /// &lt;/summary&gt;
    public readonly string Value;

    public LanguageCode(string language)
    {
        this.Value = new CultureInfo(language).TwoLetterISOLanguageName;
    }

    public static LanguageCode TryParse(string language)
    {
        if (language == null)
        {
            return null;
        }

        if (language.Length &gt; 2)
        {
            language = language.Substring(0, 2);
        }

        try
        {
            return new LanguageCode(language);
        }
        catch (ArgumentException)
        {
            return null;
        }
    }
}

public class RegionCode
{
    /// &lt;summary&gt;
    /// Get the uppercase two-character ISO 3166 region/country code.
    /// &lt;/summary&gt;
    public readonly string Value;

    public RegionCode(string region)
    {
        this.Value = new RegionInfo(region).TwoLetterISORegionName;
    }

    public static RegionCode TryParse(string region)
    {
        if (region == null)
        {
            return null;
        }

        if (region.Length &gt; 2)
        {
            region = region.Substring(0, 2);
        }

        try
        {
            return new RegionCode(region);
        }
        catch (ArgumentException)
        {
            return null;
        }
    }
}
</code></pre>

[{'answer_id': 70645, 'author': 'Chris Kimpton', 'author_id': 48310, 'author_profile': 'https://Stackoverflow.com/users/48310', 'pm_score': 2, 'selected': False, 'text': '<p>It depends, if they are not going to do much more, then I would probably leave them as is - IMHO factoring out stuff is likely to be more complex, in this case.</p>\n'}, {'answer_id': 70662, 'author': 'Henry B', 'author_id': 6414, 'author_profile': 'https://Stackoverflow.com/users/6414', 'pm_score': 0, 'selected': False, 'text': "<p>This is a rather simple question and to me smells awefully like a homework assignment.</p>\n\n<p>You can obviously see the common bits in the code and I'm pretty sure you can make an attempt at it yourself by putting such things into a super-class.</p>\n"}, {'answer_id': 70667, 'author': 'Ben', 'author_id': 5005, 'author_profile': 'https://Stackoverflow.com/users/5005', 'pm_score': 0, 'selected': False, 'text': '<p>You could maybe combine them into a <code>Locale</code> class, which stores both Language code and Region code, has accessors for Region and Language plus one parse function which also allows for strings like "en_gb"...</p>\n\n<p>That\'s how I\'ve seen locales be handled in various frameworks.</p>\n'}, {'answer_id': 70671, 'author': 'Steve Morgan', 'author_id': 5806, 'author_profile': 'https://Stackoverflow.com/users/5806', 'pm_score': 0, 'selected': False, 'text': "<p>These two, as they stand, aren't going to refactor well because of the static methods.</p>\n\n<p>You'd either end up with some kind of factory method on a base class that returns an a type of that base class (which would subsequently need casting) or you'd need some kind of additional helper class.</p>\n\n<p>Given the amount of extra code and subsequent casting to the appropriate type, it's not worth it.</p>\n"}, {'answer_id': 70673, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 0, 'selected': False, 'text': "<p>I'm sure there is a better generics based solution. But still gave it a shot. </p>\n\n<p>EDIT: As the comment says, static methods can't be overridden so one option would be to retain it and use TwoLetterCode objects around and cast them, but, as some other person has already pointed out, that is rather useless. </p>\n\n<p>How about this?</p>\n\n<pre><code>public class TwoLetterCode {\n    public readonly string Value;\n    public static TwoLetterCode TryParseSt(string tlc) {\n        if (tlc == null)\n        {\n            return null;\n        }\n\n        if (tlc.Length &gt; 2)\n        {\n            tlc = tlc.Substring(0, 2);\n        }\n\n        try\n        {\n            return new TwoLetterCode(tlc);\n        }\n        catch (ArgumentException)\n        {\n            return null;\n        }\n    }\n}\n//Likewise for Region\npublic class LanguageCode : TwoLetterCode {\n    public LanguageCode(string language)\n    {\n        this.Value = new CultureInfo(language).TwoLetterISOLanguageName;\n    }\n    public static LanguageCode TryParse(string language) {\n        return (LanguageCode)TwoLetterCode.TryParseSt(language);\n    }\n}\n</code></pre>\n"}, {'answer_id': 70684, 'author': 'Steve Cooper', 'author_id': 6722, 'author_profile': 'https://Stackoverflow.com/users/6722', 'pm_score': 0, 'selected': False, 'text': '<ol>\n<li>Create a generic base class (eg <code>AbstractCode&lt;T&gt;</code>)</li>\n<li><p>add abstract methods like</p>\n\n<pre><code>protected T GetConstructor(string code);\n</code></pre></li>\n<li><p>override in base classes like</p>\n\n<pre><code>protected override RegionCode GetConstructor(string code)\n{\n    return new RegionCode(code);\n}\n</code></pre></li>\n<li><p>Finally, do the same with <code>string GetIsoName(string code)</code>, eg</p>\n\n<pre><code>protected override GetIsoName(string code)\n{\n    return new RegionCode(code).TowLetterISORegionName;\n}\n</code></pre></li>\n</ol>\n\n<p>That will refactor the both. Chris Kimpton does raise the important question as to whether the effort is worth it. </p>\n'}, {'answer_id': 70777, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 1, 'selected': True, 'text': '<p>Unless you have a strong reason for refactoring (because you are going to add more classes like those in near future) the penalty of changing the design for such a small and contrived example would overcome the gain in maintenance or overhead in this scenario. Anyhow here is a possible design based on generic and lambda expressions.</p>\n\n<pre><code>public class TwoLetterCode&lt;T&gt;\n{\n    private readonly string value;\n\n    public TwoLetterCode(string value, Func&lt;string, string&gt; predicate)\n    {\n        this.value = predicate(value);\n    }\n\n    public static T TryParse(string value, Func&lt;string, T&gt; predicate)\n    {\n        if (value == null)\n        {\n            return default(T);\n        }\n\n        if (value.Length &gt; 2)\n        {\n            value = value.Substring(0, 2);\n        }\n\n        try\n        {\n            return predicate(value);\n        }\n        catch (ArgumentException)\n        {\n            return default(T);\n        }\n    }\n\n    public string Value { get { return this.value; } }\n}\n\npublic class LanguageCode : TwoLetterCode&lt;LanguageCode&gt;  {\n    public LanguageCode(string language)\n        : base(language, v =&gt; new CultureInfo(v).TwoLetterISOLanguageName)\n    {\n    }\n\n    public static LanguageCode TryParse(string language)\n    {\n        return TwoLetterCode&lt;LanguageCode&gt;.TryParse(language, v =&gt; new LanguageCode(v));\n    }\n}\n\npublic class RegionCode : TwoLetterCode&lt;RegionCode&gt;\n{\n    public RegionCode(string language)\n        : base(language, v =&gt; new CultureInfo(v).TwoLetterISORegionName)\n    {\n    }\n\n    public static RegionCode TryParse(string language)\n    {\n        return TwoLetterCode&lt;RegionCode&gt;.TryParse(language, v =&gt; new RegionCode(v));\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/70625', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11574/']