Skip to main content

Posts

Showing posts from February, 2009

String.Format

In Microsoft.Net most of the objects implement .ToString() function. This is a very powerful function which returns string representation of underlying object. However if it is overused with concation operators like + in C#, it can have serious performace hit for large application. The problem with concatenation is that it create a new string for every concatenation operator and assigns memory to it. If it is just string concatenation you are looking for, use StringBuilder class instead. A better and Microsoft recomended way of formatting strings is to use String.Format. This is a very powerful funation and you would be amazed by the number of formats it support. Once you get hang of it, you will never go back to using .ToString. Here is an example. Say you wany to display current date time. Your normal code would be Response.write("Todays Date is: " + DateTime.Now.ToString("dd/MM/YYYY")); A better way would be Reponse.Write(String.Format("Todays Date is:{0:dd/