Search This Blog

Common Programming Tips for C#.

Here is some most common programming errors and tips for c# developers.

Use Convert.ToString() instead of .ToString()
Convert.ToString function handles null values whereas .ToString() cannot handle null values and throw error "Object Reference not set to an instance of an object."

Use StringSplitOptions.RemoveEmptyEntries while splitting the string
When we split some string and perform some operations on each portion of string, using StringSplitOptions.RemoveEmptyEntries is a good practice this will remove empty strings from the returned Array, and remaining operations does not give an error.

You can see difference in results. In first case you need to check whether portion is not Empty, if it is it may throw an error. Also it returns count with empty strings so logic for program must change accordingly.

Use TryParse instead of Convert function for converting Int, double, boolean etc.
If the object is null or not valid value for conversion this functions throw an error while TryParse returns 0 in case of null or wrong input.

Link Within Related Posts Plugin for WordPress, Blogger...
There was an error in this gadget