Click here to Skip to main content
15,179,257 members
Articles / General Programming / String
Posted 22 Oct 2010

Tagged as


6 bookmarked

Common Mistakes Made by Programmers

Rate me:
Please Sign up or sign in to vote.
3.16/5 (21 votes)
22 Oct 2010CPOL1 min read
Common Mistakes Made by Programmers

Making mistakes is inevitable in programming. Even a small mistake could prove to be very costly. The wise thing is to learn from your mistakes and try not to repeat them.

In this article I will be highlighting the one of the mistakes which I consider to be the most common mistake made by C# developers.

Formatting a string

There lies the strong possibility of making costly mistakes while working with string types in C# programming. String is always an immutable type in the .NET Framework. When a string is modified it always creates a new copy and never changes the original. Most developers always format the string as shown in the sample below

<br />
string updateQueryText = "UPDATE EmployeeTable SET Name='" + name + "' WHERE EmpId=" + id;<br />
<br />

The above code is really messy and also as the string is immutable it creates 3 unnecessary garbage string copies in the memory as a result of multiple concatenations.

The better approach is to use string.Format as it internally uses StringBuilder which is mutable. It also paves the way for a clean code.

<br />
string updateQueryText = string.Format("UPDATE EmployeeTable SET Name='{0}' WHERE EmpId={1}", name, id);<br />

Keep watching for some more tips....


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
I have good knowledge on Application Development, Developing components, Participating in design workshops and reviews. Particularly interested in client/server and windows applications. i have working experience with Oracle 10g, 11g, PostgreSQL, and MS-SQL Server Databases.

Comments and Discussions

GeneralReason for my vote of 4 nice tip Pin
MessiNRG12-Feb-12 6:03
MemberMessiNRG12-Feb-12 6:03 
GeneralReason for my vote of 4 Good Tip Pin
Sandesh M Patil3-Dec-10 3:31
MemberSandesh M Patil3-Dec-10 3:31 
GeneralReason for my vote of 1 It's a total mistake to concatenate.... Pin
ArchElf27-Oct-10 10:32
MemberArchElf27-Oct-10 10:32 
GeneralReason for my vote of 1 bad sql script Pin
yunusatmaca27-Oct-10 4:51
Memberyunusatmaca27-Oct-10 4:51 
GeneralYes.I completely agree. Pin
GPUToaster™26-Oct-10 23:38
MemberGPUToaster™26-Oct-10 23:38 
GeneralReason for my vote of 1 If you're going to demonstrate strin... Pin
Richard Deeming26-Oct-10 9:15
mveRichard Deeming26-Oct-10 9:15 
Generaloops I was meant to say: string text = tbStatic.text + " ... Pin
Blubbo26-Oct-10 4:11
MemberBlubbo26-Oct-10 4:11 
GeneralReason for my vote of 5 Thanks for the tip! I've never used ... Pin
Blubbo26-Oct-10 4:09
MemberBlubbo26-Oct-10 4:09 
GeneralReason for my vote of 1 This is SQL injection waiting to hap... Pin
jim lahey26-Oct-10 3:16
Memberjim lahey26-Oct-10 3:16 
GeneralReason for my vote of 3 Information is not full. It contains... Pin
Anton Kratenok25-Oct-10 23:24
MemberAnton Kratenok25-Oct-10 23:24 
GeneralReason for my vote of 5 practible Pin
Semions25-Oct-10 20:07
MemberSemions25-Oct-10 20:07 
GeneralReason for my vote of 3 If you expect to execute the line of... Pin
dmjm-h25-Oct-10 13:38
Memberdmjm-h25-Oct-10 13:38 
GeneralIt may also be good to mention the StringBuilder.AppendForma... Pin
supercat925-Oct-10 7:10
Membersupercat925-Oct-10 7:10 
GeneralStringBuilder isn't magic. If it has to expand its internal ... Pin
Henry.Ayoola25-Oct-10 4:58
MemberHenry.Ayoola25-Oct-10 4:58 
GeneralReason for my vote of 2 it's a good pract Pin
Herre Kuijpers23-Oct-10 4:18
MemberHerre Kuijpers23-Oct-10 4:18 
Generalmy intension is mainly using string.Format() Method.. we can... Pin
TweakBird23-Oct-10 3:58
MemberTweakBird23-Oct-10 3:58 
Generalsensible to SQL injection. Pin
Hiren solanki22-Oct-10 23:32
MemberHiren solanki22-Oct-10 23:32 
GeneralReason for my vote of 4 Nice use of string.Format Thanks for... Pin
Khaniya22-Oct-10 20:28
professionalKhaniya22-Oct-10 20:28 
GeneralYou must have mist this comic that humorously makes clear wh... Pin
E.F. Nijboer22-Oct-10 12:54
MemberE.F. Nijboer22-Oct-10 12:54 
GeneralReason for my vote of 1 I gave you a 1 because your example ... Pin
SledgeHammer0122-Oct-10 11:09
MemberSledgeHammer0122-Oct-10 11:09 
GeneralReason for my vote of 5 never knew stringbuilder is used ins... Pin
FadiYoosuf22-Oct-10 5:10
MemberFadiYoosuf22-Oct-10 5:10 
GeneralReason for my vote of 4 Thanks for the info. It is really a ... Pin
Sivaraman Dhamodharan22-Oct-10 4:10
MemberSivaraman Dhamodharan22-Oct-10 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.