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

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer We had very good experience with storing the config in… May 11, 2026 at 7:34 am
  • added an answer Take a look at the Adobe Flash Media Server Administration… May 11, 2026 at 7:34 am
  • added an answer sed 's/\([A-Z]\)/ \1/g' Captures each capital letter and substitutes a… May 11, 2026 at 7:34 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.