Using Sting.Format instead of building a string through manual concatenation

28 Feb

when you use concatenation to build a string instead of string format c# creates (a) extra String Object(s) so instead use this it is more efficent overall

instead of

cmd.CommandText = "SELECT * FROM addresses WHERE Zip LIKE '" + delZip.Trim() + "%'";

Do this

cmd.CommandText = String.Format("SELECT * FROM addresses WHERE Zip LIKE '{0}%'", delZip.Trim());

The code is more readable you have one string that is legible without all the breaks and pluses and the like. This allows me to read the string and then see what variables are being put into the placeholders (probably not the correct technical term but you get the point).

Leave a comment

Posted by on February 28, 2012 in C#


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: