Globalization and culture

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

I just wonder these two rows 1 and 2 below give identical output for those
culture that I have tested with.
So my first question is if rows 1 and 2 are the same ?
My second question is how is it possible that these two rows give identical
output when I pass in two completely different object ?
In the first example I pass in a CultureInfo
In the second example you pass in a DateTimeFormatInfo

1. Console.WriteLine(DateTime.Now.ToString("D", new CultureInfo("sv-fi")));
2. Console.WriteLine(DateTime.Now.ToString("D", new
CultureInfo("sv-FI").DateTimeFormat));

//Tony
 
Tony Johansson said:
I just wonder these two rows 1 and 2 below give identical output for those
culture that I have tested with.
So my first question is if rows 1 and 2 are the same ?
My second question is how is it possible that these two rows give
identical output when I pass in two completely different object ?
In the first example I pass in a CultureInfo
In the second example you pass in a DateTimeFormatInfo

1. Console.WriteLine(DateTime.Now.ToString("D", new
CultureInfo("sv-fi")));
2. Console.WriteLine(DateTime.Now.ToString("D", new
CultureInfo("sv-FI").DateTimeFormat));

You are invoking an overload of the ToString method that takes an
IFormatProvider. Both CultureInfo and DateTimeFormatInfo implement that
interface. This interface declares a method called GetFormat(). If you are
seeing the same result from boths calls, it means that the developers of
both classes chose to implement the interface in such a way that the
GetFormat method is returning the same format in both cases.
 
Excellent explained !!

//Tony

Alberto Poblacion said:
You are invoking an overload of the ToString method that takes an
IFormatProvider. Both CultureInfo and DateTimeFormatInfo implement that
interface. This interface declares a method called GetFormat(). If you are
seeing the same result from boths calls, it means that the developers of
both classes chose to implement the interface in such a way that the
GetFormat method is returning the same format in both cases.
 
Back
Top