Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

.net - What is the difference between Culture and UICulture?

Could someone give me a bit more information on the difference between Culture and UICulture within the .NET framework? What they do and when to use what?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Culture affects how culture-dependent data (dates, currencies, numbers and so on) is presented. Here are a few examples:

var date = new DateTime(2000, 1, 2);
var number = 12345.6789;

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine(date); // 02.01.2000 00:00:00
Console.WriteLine(number.ToString("C")); // 12.345,68 €

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
Console.WriteLine(date); // 2000-01-02 00:00:00
Console.WriteLine(number.ToString("C")); // 12 345,68 $

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine(date); // 1/2/2000 12:00:00 AM
Console.WriteLine(number.ToString("C")); // $12,345.68

Culture also affects parsing of user input in the same way:

const string numberString = "12.345,68";
decimal money;

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
money = decimal.Parse(numberString); // OK!

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
money = decimal.Parse(numberString); // FormatException is thrown, TryParse would return false

Beware of cases where the parsing succeeds but the result is not what you would expect it to be.

const string numberString = "12.345";
decimal money;

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
money = decimal.Parse(numberString); // 12345

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
money = decimal.Parse(numberString); // 12.345, where the . is a decimal point

UICulture affects which resource file (Resources.lang.resx) is going to be loaded to by your application.

So to load German resources (presumably localized text) you would set UICulture to the German culture and to display German formatting (without any impact on which resources are loaded) you would set Culture.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...