Tag Archives: C#

System.Reflection.TargetInvocationException

When you get a popup about this kind of exception in C#, select Break in the popup and then look in your Locals for the innerException property of this exception.  It will probably indicate something like “Exception thrown by type initializer of class“, which means you might be trying to do something like get the Length of an uninitialized string.

Using curly braces in a format string in C#

Because curly braces are used in C# to indicate where values should be pasted in, like this…

sb.AppendFormat(“Email: {0}”, sEmail);

…therefore, if you need to display curly braces in the final string, you’ll need to escape them, like this:

sb.AppendFormat(“var oContact = {email:'{0}’}“, sEmail);

“System.FormatException: Input string was not in a correct format.”

When using .Net’s StringBuilder, you might see this error. The likely cause is that you have a curly brace in the format string that you want to appear in the final string.  That will mislead .Net into thinking it’s a placeholder (the expected use of curly braces), and escaping with doesn’t seem to help.

Break up the format string so that the “literal” curly braces appear only in Append’s.

Read “Effective Java” book to improve skills in Java or C#

Although my knowledgeable programmer friends seem to have known about this book for awhile, I discovered Joshua Bloch’s book “Effective Java” just recently.  This book is chock-full of great advice that applies not just to Java but also C# and the C family. If you read just one book about programming, I recommend this one.

I have heard, and strongly suspected myself, that C# is just a layer on top of Microsoft’s old implementation of Java. So it’s well-worth understanding Java even if you consider yourself dedicated to C# (which doesn’t seem sensible to me, but you may have your reasons).