A Read-Only, Modified CultureInfo?

by Michael S. Kaplan, published on 2006/02/14 03:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2006/02/14/531621.aspx

Elliot asked:

Apologies if this is the wrong alias for this question or if this question has been asked before.  I’m running into some strange behavior and I’m trying to figure out if I’m doing something wrong, if this is by design, or if there may be some other problem.  I find that when I call the ReadOnly() method on a CultureInfo object it resets the ShortDatePattern of the internal DateTimeFormatInfo object to “M/d/yyyy” even if I’ve set it differently.  So, for example: 

CultureInfo ci = new CultureInfo("en-US");
ci.DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
ci = CultureInfo.ReadOnly(ci);

// ci.DateTimeFormat.ShortDatePattern is now "M/d/yyyy"

I can’t figure out why this is happening.  I could understand if when making the CultureInfo read only the CultureInfo confirms that the various string patterns are valid and changes them to defaults if they’re not, but "yyyy/MM/dd" seems like a valid ShortDatePattern.  Anyone know what’s going on here?

The issue Elliot is hitting is a side effect of the way that the static method CultureInfo.ReadOnly is implemented. You see, it is not making an entirely shallow copy since it is asking for (for example) read-only instances of the child CultureInfo.DateTimeFormat and CultureInfo.NumberFormat objects via their respective static DateTimeFormatInfo.ReadOnly and NumberFormatInfo.ReadOnly methods.

But, contrary to Elliot's hopes, it is not making a deep copy of those child objects or their properties, either.

I guess you could say that it is making a semi-shallow copy? :-)

There is not really a "deep copy" method other than manually setting all of the properties that are customized... and there is no way to make such an object read-only, once you do it.

Unless you count creating a read-only object that is based on the culture that goes with the default user locale on Windows and you are allowing user overrides. Beyond that, you are on your own....

Well, that is other than creating a custom culture, which you can then instantiate as a read-only object. Though I must admit that feels a bit like overkill to me (and not always practical).

But the notion of overriding and then marking a culture read-only does seem to violate the real intent of the CultureInfo class, which is provide expected cultural preferences except in those scenarios when you might be providing a custom culture overriding properties that you have good reason to override....


This post brought to you by "" (U+1818, MONGOLIAN DIGIT EIGHT)

# anbuPhoenix on 14 Feb 2006 3:27 AM:

No Comments as of now

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day