Top C# Programming Mistakes
1.Using String variables:
if (someString.Length > 0)
{
// …
}
… but someString could be a null:
if (!String.IsNullOrEmpty(someString))
{
// much better now !?
}
2.String concatenation
string s = “dev”;
s += “-”;
s += “the”;
s += “-”;
s += “web”;
s += “.”;
s += “com”;
This is not very effective and the program will work slower instead of using StringBuilder class and it’s Append method:
StringBuilder sb = new StringBuilder();
sb.Append(“dev”);
sb.Append(“-”);
sb.Append(“the”);
sb.Append(“-”);
sb.Append(“web”);
sb.Append(“.”);
sb.Append(“com”);
3.Using Console
Console.WriteLine(“A= ” + 1 + ” B=” + 2 + ” C= ” + someValue);
same as 2. it’s not very effective. What about this:
Console.WriteLine(“A: {0}nB: {1}nC: {2}”, 1, 2, someValue);
4.Parse Integer value from string variable
int i = int.Parse(Request.QueryString["id"]);
but if we’ve a url like this yourpage.aspx?id=A6 the above method will throw an exception. What about TryParse method?
int i;
if (!int.TryParse(Request.QueryString["id"] , out i))
{
//…
}
5. Calling IDbConnection’s Close method
IDbConnection dbConn = null;
try
{
dbConn = new SqlConnection(“some Connection String”);
dbConn.Open();
}
finally
{
dbConn.Close();
}
Calling SqlConnection’ constructor may throw an Exception, should we call Close method then !?
IDbConnection dbConn = null;
try
{
dbConn = new SqlConnection(“Some Connection String”);
dbConn.Open();
}
finally
{
if (dbConn != null)
{
dbConn.Close();
}
}
6.Using List class
public void SomeMethod(List<SomeItem> items)
{
foreach(var item in items)
{
// do something with the item…
}
}
If we just loop through the List of items. In this case, requiring a list is an over specification, so it could done in this way:
public void SomeMethod(IEnumerable<SomeItem> items)
{
foreach(var item in items)
{
// do something with the item…
}
}
7.Use numbers in more that simple numbers way
if(mode == 1) { … }
else if(mode == 2) { … }
else if(mode == 3) { … }
Why don’t we use Enumerations in this case:
public enum SomeEnumerator
{
DefaultMode = 1,
SafeMode = 2,
NormalMode = 3
}
if(mode == SomeEnumerator.DefaultMode) { … }
else if(mode == SomeEnumerator.SafeMode) { … }
else if(mode == SomeEnumerator.NormalMode) { … }
8.String Replace
string s = “www.DevTheWeb.NET is a cool site”;
s.Replace(“cool”, “awful”);
Nothing changed in s’ value, it’s still “www.DevTheWeb.NET is a cool site”
s = s.Replace(“cool”, “awful”);
Now s’ value is realy changed.
I hope you’ll like the list of common C# mistakes
P.S. If you know some cool C# mistake, please, post a comment about it


11 Comments to “Top C# Programming Mistakes”
Is there a compiler-time optimization in .net for string concatenation like in java? if not, there is a mistake from .net compiler…
For #5
This is equivalent and much cleaner.
using(dbConn = new SqlConnection(”Some Connection String”))
{
dbConn.Open();
}
In number 5 move
dbConn = new SqlConnection(”Some Connection String”);
outside the try statement
dbConn = new SqlConnection(”Some Connection String”);
try
{
….
}
finally
{
dbConn.Close;
}
Or better yet use the using statement
Good list. However point no 2 actually depends on the situation. If you are concatenating a relatively small (4 – 6) no of strings together then using StringBuilder is actually an overhead.
However, if your concatenation is being done in a loop, then you definitely should use StringBuilder.
You forgot my pet peeve.
try{..}
catch(Exception e)
{
throw e;
}
now you just lost all the context of where the exception really happened.
@Manoel If you do the *string concatenation* when you define a string I’m pretty sure that the C# compiler optimises this at compile time so that only 1 string is created. So for no. 2 you should be able to do this if you don’t fancy using a StringBuilder:
string s = “dev” + “-” + “the” + “-” + “web” + “.” + “com”;
Using auto-complete to write this:
dbConn.Clone();
Instead of:
dbConn.Close();
Well, I don’t know how common this is, but it happened to me once and I spent a lot of debugging time catching this one.
If the connection’s scope is local, then using “using” is the best solution, but sometimes the connection is shared between scopes, so we need to explicitly close the connection.
#11 – Chose .NET over Java
[...] Read more: Dev-The-Web’s Blog [...]
1. You realise that these two statements are not equivalent presumably? You also realise that MS guidelines suggest the former when checking for an empty (not null) string? Though I would use != rather than >.
2 and 3 are the same thing but with this number of strings its debatable whether it is quicker to use a StringBuilder / Formatter or concatenation operator. In 3 I agree but in 2, your second example coudl be argued to be less readble.
4. This isn’t a mistake. It’s a matter of context. Why use TryParse in situations where you know that the string contains a number?
5. Using.
6. This again is a matter of context. If you know you will be using a List member at some point in the future, why set yourself up for a breaking change?
7. I’m not even getting started on this one. There’s so many things wrong with it. (Though the use of enumerations isn’t one of them).
8. Well, duh!
Thats very good to know… thanks