The Visual Studio 2013 debugger is behaving in an unexpected way after applying .ToString() to a StringBuilder. In the controller, I'm attempting to intercept some errors, and have adapted some code from this SO post. How to get all Errors from ASP.Net MVC modelState?
But when the debugger runs, and after a .ToString() action is applied to the StringBuilder, I'm unable to inspect the contents of the string via Watch or hovering over the string.
string theErrors = string.Empty;
if (!ModelState.IsValid)
{
StringBuilder sb = new StringBuilder();
foreach (ModelState modelState in ViewData.ModelState.Values)
{
foreach (ModelError error in modelState.Errors)
{
sb.AppendFormat("{0}{1}", error.ErrorMessage, Environment.NewLine);
}
}
theErrors = sb.ToString();
sb = null;
}
If I place a breakpoint after this code, and try to inspect the "theErrors" string, Visual Studio debugger displays this message.
"The name 'theErrors' does not exist in the current context."
This is unexpected behavior. Am I doing something wrong with the StringBuilder? I use StringBuilder frequently, and haven't seen this before. Thanks for your help.