Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format exception following upgrade from .Net 2.0 to .Net 4.5.2

Tags:

c#

.net

asp.net

I have recently upgraded a ASP.Net forms application to .Net 4.5.2, after fixing some relatively trivial namespace issues I was able to build the solution successfully. However at runtime I have been receiving the following error:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Which when debugging is thrown by the following line:

string.Format("Init took {0:mm:ss}", (object) DateTime.Now.Subtract(renderStartTime))  

Where renderStartTime = DateTime.Now

I am somewhat puzzled why I am seeing this error since upgrading. Any thoughts?

like image 223
Matt Avatar asked Dec 02 '14 16:12

Matt


People also ask

What is string format in C#?

In C#, Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.

What is format exception in C#?

A FormatException exception can be thrown for one of the following reasons: In a call to a method that converts a string to some other data type, the string doesn't conform to the required pattern. This typically occurs when calling some methods of the Convert class and the Parse and ParseExact methods of some types.

Which exception may be thrown when a user inputs a string when the application expected a decimal number?

The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown.

Can we convert. net Framework to. net Core?

You can migrate your old project to the Core project using the 'dotnet migrate; command, which migrates the project. json and any other files that are required by the web application. The dotnet migrate command will not change your code in any way.


1 Answers

It is a breaking change.

See: Formatting and Parsing Time Intervals in the .NET Framework 4

In the .NET Framework 3.5 and earlier versions, TimeSpan does not implement IFormattable, nor does it support format strings. Therefore, the “r” format string is ignored, and the parameterless TimeSpan.ToString method is called. In the .NET Framework 4, on the other hand, TimeSpan.ToString(String, IFormatProvider) is called and passed the unsupported format string, which causes the exception.

Just to expand on the answer, your original code in .Net framework 2.0 would not throw an exception but it will not give you the desired output, (minutes:seconds). Since the parameter less constructor would be call for TimeSpan, ignoring the format specified in String.Format. But, with .Net framework 4.0 or hgiher , since TimeSpan implements IFormattable, the format specified mm:ss would be passed to the ToString call. Now this format mm:ss is invalid for TimeSpan, it requires colon to be escaped with back slash like: mm\:ss. That is why you are getting exception.

See: Custom TimeSpan Format Strings

In .Net 3.5 or lower you can use:

TimeSpan elapsed = DateTime.Now - renderStartTime; //or DateTime.Now.Subtract(renderStartTime)
string formatted = string.Format("Init took {0}:{1}", elapsed.Minutes, elapsed.Seconds); //returns minutes and seconds components, 
                                        // If you are looking for Total Minutes and Total Seconds then use TotalMinutes/TotalSeconds

In .Net framework 4.0 or higher you can do:

string.Format("Init took {0:mm\\:ss}", elapsed);
like image 127
Habib Avatar answered Nov 15 '22 23:11

Habib