DateTimeFormatInfo.InvariantInfo vs CultureInfo.InvariantCulture

I am trying to parse DateTime, with the accepted exact format from client input.

Which one is better

bool success = DateTime.TryParseExact(value, "dd-MMM-yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateTime); 

OR

 bool success = DateTime.TryParseExact(value, "dd-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime); 

Of course, this code is inside a common static method, which is called wherever parsing the date is required.

+6
optimization c # datetime
source share
2 answers

If you look at the signature for DateTime.TryParseExact , IFormatProvider is required as the third argument. Both DateTimeFormatInfo.InvariantInfo and CultureInfo.InvariantCulture implement this interface, so you actually call the same method on DateTime in both cases.

Internally, if you use CultureInfo.InvariantCulture , its DateTimeFormat property is called to get an instance of DateTimeFormatInfo . If you use DateTimeFormatInfo.InvariantInfo , this is used directly. Calling DateTimeFormatInfo will be a little faster, as it should execute fewer instructions, but it will be so insignificant as to make no difference (almost) in all cases.

The main difference between the two approaches is the syntax. Use the one that you find most clear.

+7
source share

They will give the same results.
And there will hardly be any performance difference.

Therefore, use everything that you consider the most readable. My choice would be DateTimeFormatInfo.InvariantInfo for being a bit more accurate.

+3
source share

All Articles