Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 44359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:38:23+00:00 2026-05-10T15:38:23+00:00

How would you refactor these two classes to abstract out the similarities? An abstract

  • 0

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?

public class LanguageCode {     /// <summary>     /// Get the lowercase two-character ISO 639-1 language code.     /// </summary>     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 > 2)         {             language = language.Substring(0, 2);         }          try         {             return new LanguageCode(language);         }         catch (ArgumentException)         {             return null;         }     } }  public class RegionCode {     /// <summary>     /// Get the uppercase two-character ISO 3166 region/country code.     /// </summary>     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 > 2)         {             region = region.Substring(0, 2);         }          try         {             return new RegionCode(region);         }         catch (ArgumentException)         {             return null;         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:38:24+00:00Added an answer on May 10, 2026 at 3:38 pm

    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.

    public class TwoLetterCode<T> {     private readonly string value;      public TwoLetterCode(string value, Func<string, string> predicate)     {         this.value = predicate(value);     }      public static T TryParse(string value, Func<string, T> predicate)     {         if (value == null)         {             return default(T);         }          if (value.Length > 2)         {             value = value.Substring(0, 2);         }          try         {             return predicate(value);         }         catch (ArgumentException)         {             return default(T);         }     }      public string Value { get { return this.value; } } }  public class LanguageCode : TwoLetterCode<LanguageCode>  {     public LanguageCode(string language)         : base(language, v => new CultureInfo(v).TwoLetterISOLanguageName)     {     }      public static LanguageCode TryParse(string language)     {         return TwoLetterCode<LanguageCode>.TryParse(language, v => new LanguageCode(v));     } }  public class RegionCode : TwoLetterCode<RegionCode> {     public RegionCode(string language)         : base(language, v => new CultureInfo(v).TwoLetterISORegionName)     {     }      public static RegionCode TryParse(string language)     {         return TwoLetterCode<RegionCode>.TryParse(language, v => new RegionCode(v));     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would you recommend as the best way to refactor these two bits of
I have got a very nasty piece of code i would like to refactor
I have a class in PHP like so: class ParentClass { public function __construct($arg)
I would like to have a column in my DB accessible via two column
I have an abstract base class which acts as an interface. I have two
I have trouble finding way to correctly refactor this code so that there would
how would I refactor this code for ARC: - (UIGestureRecognizer *)createTapRecognizerWithSelector:(SEL)selector { return [[[UITapGestureRecognizer
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden
So, I have a class. It's a useful class. I like a lot. Let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.