Overview
Microsoft released the new version of .Net Framework, .NET Framework 4.6, with
Visual Studio 2015 Preview on November 12, 2014. It was announced on the day of the
Visual Studio Connect() event from New York, USA. In this article, we will learn about some small changes to the .NET Base Class Libraries (BCL). So before becoming familiar with the changes in the Base Class Libraries we should be familiar with it.
Base Class Libraries
The .NET Framework Base Class Libraries is a library of classes, interfaces, and value types that provide access to system functionality. We can say that it is the foundation of the .NET Framework on which the applications, controls, and components of the .NET Framework are built. In .NET Framework 4.6 many new APIs have been added to enable the following key scenarios.
- Some CultureInfo properties are read-write rather than read-only.
Some CultureInfo properties are read-write rather than read-only
The
CultureInfo.CurrentCulture and
CultureInof.CurrentUICulture properties are now
read-write rather than read-only. In previous versions of the .NET Framework, these are read-only properties so we couldn't create any specific culture but now it has become possible to create a specific culture. So before trying to understand the changes in these two properties we should be familiar with the basics of these properties.
The
System.Globalization is
the namespace that contains the definition for the
CultureInfo class that defines culture-related information like languages, country/region, calendars, currency, and numbers. All the classes that come under the System.Globalization namespace is useful for developing globalized applications. The CultureInfo class has very many properties, some of them are listed below:
- CurrentCulture
- CurrentUICulture
- CultureTypes
- CurrentCulture
- Calendar
- DateTimeFormat
- EnglishName
- InvariantCulture
- StringInfo
- TextInfo
- and more...
Notes
There are many properties available for the
CultureInfo class but in this article, we will only explore those of which some changes were made by Microsoft. So the following are the two properties in which changes were made, CurrentCulture and CurrentUICulture.
CurrentCulture
The CurentCulture property is used to get or set the current culture used by the current thread. In other words in .NET Framework 4.6, it became a read-write property. It was a read-only property before the release of .NET Framework 4.6 and v4.5.2. It is responsible for formatting and parsing the values of the current thread.
Example I (with .NET Framework 4.6 in Visual Studio 2015 Preview):
- namespace TestConsoleApplication {
- class ProgramInNETFramework4pont6 {
- static void Main(string[] args) {
-
-
- Console.WriteLine("Current Culture: " + CultureInfo.CurrentCulture);
- Console.WriteLine("Current Name (in English)" + CultureInfo.CurrentCulture.EnglishName);
- Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "Amount: {0:C2}", 25000));
- Console.WriteLine("Today is : " + CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(DateTime.Now));
-
-
- CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-IN");
- Console.WriteLine("Current Culture: " + CultureInfo.CurrentCulture);
-
- CultureInfo.CurrentCulture.EnglishName = CultureInfo.CreateSpecificCulture("English <India>");
- Console.WriteLine("Culture Name (in English) " + CultureInfo.CurrentCulture);
-
- CultureInfo.CurrentCulture.EnglishName = CultureInfo.CreateSpecificCulture("English <India>");
- Console.WriteLine("Culture Name (in English) " + CultureInfo.CurrentCulture);
- }
- }
- }
Note:
The preceding program written in .NET Framework 4.6 is well, there is no issue of the read-only property but when I write the same program in
v4.5 and
v4.0 both will produce some syntax error like: "
Can not be assigned to read-only property".
I would like to share that this feature is not working properly with each Visual Studio 2015 Preview but it has been announced by Microsoft. If this code is not working in your system, there is nothing to worry about because this preview version is released for testing purposes. It will however work with the final release.
See
Let's see it practically!
Example II (with .NET Framework 4.5 in Visual Studio 2013):
Now we will write the same program in .NET Framework 4.5, it will produce an error!
It's saying "It's a read-only property",
System.Globalization.CultureInfo.CurrentCulture can not be assigned.
System.Globalization.CultureInfo.EnglishName can not be assigned.
Example III (with .NET Framework 4.0 in Visual Studio 2010):
Again we will write the same program in .NET Framework 4.0, it will also produce an error!
Note:
It's saying "It's a read-only property",
System.Globalization.CultureInfo.CurrentCulture can not be assigned.
System.Globalization.CultureInfo.EnglishName can not be assigned.
CurrentUICulture
CurrentUICulture represents the current culture of the application used by resource files (.resx files). It was a read-only property before the release of .NET Framework 4.6 and v4.5.2. Now it became a read-write property with .NET Framework 4.6. It is responsible for formatting and parsing the values of the current thread. Let's see it with some example programs.
Example I (with .NET Framework 4.6 in Visual Studio 2015 Preview):
[ PROGRAM ]
*OutputImage
Example II (with .NET Framework 4.5 in Visual Studio 2013):
Example III (with .NET Framework 4.0 in Visual Studio 2010):
Summary
In this article, we learned about the changes to the Base Class Libraries especially in the CurrentUICulture and CurrentCulture properties of the CultureInfo class in the namespace System.Globalization. Thanks!