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

  • SEARCH
  • Home
  • 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 765865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:57:16+00:00 2026-05-14T16:57:16+00:00

I have a lot of code in a class library that does not specify

  • 0

I have a lot of code in a class library that does not specify CultureInfo.InvariantCulture. For example in toString operations, toBool, toInt, etc.

Is there a way I can get set a property for the class library to always execute using CultureInfo.InvariantCulture, even if it is not explicitly specified everywhere in the code?

Sort of like a global switch?

It is not only messy to have to explicitly type it everytime, it makes my code less readable, and is a royal pain for example:

if (Convert.ToInt16(task.RetryCount, CultureInfo.InvariantCulture) <
            Convert.ToInt16(ConfigurationManager.AppSettings["TasksMaxRetry"], CultureInfo.InvariantCulture))
  • 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. Editorial Team
    Editorial Team
    2026-05-14T16:57:16+00:00Added an answer on May 14, 2026 at 4:57 pm

    While I agree that Mark’s answer is the correct answer to the question posed; I don’t think that switching the thread culture is a good design. It could introduce subtle bugs if other parts of the application, most likely the UI, depends on the threads current culture. Also, I would argue that explicitly stating the culture in Convert calls is a good design, that tells the reader of the code that the original programmer has made an active decision about which format to allow; and that the code is not just “working by coincidence”.

    You will most likely want to have many of your parse operations grouped together in the same class; perhaps one that deals with reading configuration. In that class, you could define a field to contain the culture you would like to use for parsing:

    private static readonly IFormatProvider parseFormat = CultureInfo.InvariantCulture;
    

    Then use that field in any calls to Convert methods or similar. Declaring the field as an IFormatProvider, together with a well chosen name, tells the reader of the code very explicitly, that this is a field used to define the parsing format. IMHO, it makes the intent of the code clearer.

    Another way to do this would be to make your own Parse / Convert class, that wraps the Convert.ToXxx methods and calls them with the format you intend to use. Then you will have the desired benefit of not having to explicitly state the format in each call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.