Common mistakes of developer

String concatenation instead of StringBuilder

String concatenation works in such a way that every time when you add something to a string, a new address in the memory is being allocated. The previous string is copied to a new location with the newly added part. This is inefficient. On the other hand we have StringBuilder which keeps the same position in the memory without performing the copy operation.

  1. //INCORRECT  
  2.   
  3. List<string> values = new List<string> { "This ""is ""Abrar ""!" };  
  4.   
  5. string outputValue = string.Empty;  
  6.   
  7. foreach (var value in values)  
  8.   
  9. {  
  10.   
  11. outputValue += value;  
  12.   
  13. }  
  14.   
  15. //CORRECT  
  16.   
  17. StringBuilder outputValueBuilder = new StringBuilder();  
  18.   
  19. foreach (var value in values)  
  20.   
  21. {  
  22.   
  23. outputValueBuilder.Append(value);  
  24.   
  25. }  
LINQ – ‘Where’ with ‘First’ instead of FirstOrDefault

A lot of programmers find a certain set of elements by means of ‘Where’ and then return the first occurrence. This is inappropriate, because the ‘First’ method can be also applied with the ‘Where’ condition. What’s more, it shouldn’t be taken for granted that the value will always be found. If “First” is used when no value is found, an exception will be thrown. Thus, it’s better to use FirstOrDefault instead. When using FirstOrDefault, if no value has been found, the default value for this type will be returned and no exception will be thrown.

  1. //INCORRECT  
  2.   
  3. List<int> numbers = new List<int> { 1, 4, 5, 6, 2, 3, 4, 5, 6, 3, 6, 23, 23 };  
  4.   
  5.   
  6. return numbers.Where(x => x == 6).First();  
  7.   
  8. //PARTLY CORRECT  
  9.   
  10. return numbers.First(x =>x==6);  
  11.   
  12. //CORRECT  
  13.   
  14. return numbers.FirstOrDefault(x => x == 6);  
Linq Do not use OrderBy before Where
  1. //INCORRECT  
  2.   
  3. var allIndiaEmployee =EmployeeList.OrderBy(x => x.EmployeeName)  
  4.   
  5. .Where(x => x.Country == Country.India);  
  6.   
  7. //CORRECT  
  8.   
  9. var allIndiaEmployee = EmployeeList.Where(x => x.Country == Country.India)  
  10.   
  11. .OrderBy(x => x.EmployeeName);  
Linq Difference between First() and Single()

Always remember that First() returns the very first item of the sequence, if no item exists it throws InvalidOperationException, whereas Single() Returns the only item in the sequence, if no item exists it throws InvalidOperationException,and if more than one item exists, it also throws InvalidOperationException.

Casting by means of ‘(T)’ instead of ‘as (T)’ when possibly not castable

  1. //INCORRECT  
  2.   
  3. var chair = (Chair)objFurniture;  
  4.   
  5. //CORRECT  
  6.   
  7. var chair = objFurniture as Chair;  
Incorrect exceptions re-throwing

C# programmers usually forget that when they throw an exception using „throw ex” they loose the stack trace. It is then considerably harder to debug an application and to achieve appropriate log messages. When simply using „throw” no data is lost and the whole exception together with the stack trace can be easily retrieved.

  1. //INCORRECT  
  2.   
  3. try  
  4.   
  5. {  
  6.   
  7. //some code that can throw exception [...]  
  8.   
  9. }  
  10.   
  11. catch (Exception ex)  
  12.   
  13. {  
  14.   
  15. //some exception logic [...]  
  16.   
  17. throw ex;  
  18.   
  19. }  
  20.   
  21. //CORRECT  
  22.   
  23. try  
  24.   
  25. {  
  26.   
  27. //some code that can throw exception [...]  
  28.   
  29. }  
  30.   
  31. catch (Exception ex)  
  32.   
  33. {  
  34.   
  35. //some exception logic [...]  
  36.   
  37. throw;  
  38.   
  39. }  
Take advantage of string.IsNullOrEmpty() and string.IsNullOrWhiteSpace()
  1. // INCORRECT:  
  2.   
  3. if (name != null && name != string.Empty)  
  4.   
  5. {  
  6.   
  7. [...]  
  8.   
  9. }  
  10.   
  11. //CORRECT  
  12.   
  13. if (string.IsNullOrEmpty(name))  
  14.   
  15. {  
  16.   
  17. [...]  
  18.   
  19. }  
Sometimes we want to make sure that a string is not only not empty and not null, but also does not comprise of whitespaces. In such situation we could of course use the following code:

  1. // INCORRECT:  
  2.   
  3. if (string.IsNullOrEmpty(name.Trim()))  
  4.   
  5. {  
  6.   
  7. [...]  
  8.   
  9. }  
  10.   
  11. //CORRECT  
  12.   
  13. if (string.IsNullOrWhiteSpace(name))  
  14.   
  15. {  
  16.   
  17. [...]  
  18.   
  19. }  

Ebook Download
View all
Learn
View all