我什么时候应该指定的CurrentCulture和InvariantCulture的,我应该离开时未指定它? [英] When should I specify CurrentCulture or InvariantCulture and when should I leave it unspecified?

查看:390
本文介绍了我什么时候应该指定的CurrentCulture和InvariantCulture的,我应该离开时未指定它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是指定的CurrentCulture和InvariantCulture的,而不是指定文化在所有的最佳做法?

What is the best practice for specifying CurrentCulture or InvariantCulture and not specifying the culture at all?

从我已阅读,如果你正在做的序列化,例如,你需要InvariantCulture的作为指定数据值的规范表示的一种手段。这就是文化为基础的字符串操作的比例相对较小。

From what I have read, if you're doing serialization, for instance, you need InvariantCulture as a means of specifying a canonical representation of a data value. That's a relatively small percentage of culture-based string manipulations.

我觉得很长,冗长和丑陋的大部分时间,我每次做的时候指定它,说:

I find it long, verbose, and ugly most of the time to specify it every time I do, say:

var greeting = string.Format(CultureInfo.CurrentCulture, "Hello ", userName); 



不过,我的团队最近打开的FxCop上现在有一个推动经常使用的CultureInfo无处不在。什么是结合简洁,可读性和功能最好的技术。

However, my team recently turned FxCop on and now there's a push to always use CultureInfo EVERYWHERE. What is the best technique to combine brevity, readability, and functionality?

有一些很好的阅读材料:?

Some good reading material:

  • Using the InvariantCulture Property
  • CultureInfo Class

推荐答案

有一种内在的权衡这里打球。

There is an inherent trade-off in play here.

至少,你要指定的CultureInfo使用时,你正在做什么程序的内部InvariantCulture的。例如,使用该序列化迫使数据表示永远是一样的,所以你不必担心国际化问题与您的内部数据格式。

At a minimum, you'll want to specify CultureInfo to use InvariantCulture whenever you are doing anything internal within your program. For example, using this with Serialization forces the data representation to always be the same, so you don't have to worry about internationalization issues with your internal data formats.

话虽这么说,到处指定此有一定的优势 - 主要是在强迫你,确保你正确地处理这方面。内部程序的工作与UI的工作需要有指定的不同的文化(前提是你要正确地本地化您的应用程序)。其结果是,一个复杂的程序往往需要此被到处指定,如在离开默认是危险充其量,并趋向于引入随时间的错误。

That being said, specifying this everywhere has some advantages - mainly in terms of forcing you to make sure you're handling this correctly. Internal program work vs. UI work needs to have a different culture specified (provided you want to properly localize your application). As a result, a complex program tends to require this to be specified everywhere, as leaving the "default" is dangerous at best, and tends to introduce bugs over time.

但是,指定这一点,因为你注意到没有,往往会增加代码的大小,并有可能降低的可读性。这导致权衡 - 通过被更加明确通过无处不在更短的代码与适当的国际化和本地化和可维护性的可读性和可维护性。

However, specifying this, as you noticed, tends to increase the size of your code, and potentially reduce the readability. This leads to the trade-off - readability and maintainability via shorter code vs. proper internationalization and localization and maintainability via being more explicit everywhere.

在我看来,没有正确的答案在这里 - 这真的取决于你的应用。如果你的应用是完全关于介绍,并没有做大量的数据处理,尤其是不与任何类型的自我管理的文件存储,设置当前文化(和UI文化)一次可被罚款。我发现,更复杂的应用往往会以这种方式不工作,以及,但是,在这种情况下,指定此的FxCop的建议似乎到处更具吸引力。

In my opinion, there is no "right" answer here - it really depends on your application. If your application is completely about presentation, and not doing a lot of data manipulation, especially not with any type of self-managed file storage, setting the current culture (and ui culture) once may be fine. I've found that more complicated applications tend to not work as well in this fashion, however, in which case the FxCop suggestions of specifying this everywhere seem more attractive.

这篇关于我什么时候应该指定的CurrentCulture和InvariantCulture的,我应该离开时未指定它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆