Mistakes are Made by C# Programmers : How You Can Avoid Them?

What common mistakes are made by C# programmers and how you can avoid them?

programming_mistake

  • Using a reference like a value or using value like a reference

This usually happens in case of a novice C# programmer. If you don’t know about the object and can’t differentiate in between value type and reference type, you could run into some surprises.

A

In the above image, both the Point and Pen objects were made in similar manner, however the Point1 value remained constant when X coordinate value was assigned to Point2. But the Pen1 was changed while developer assigned new color to Pen2.

How to know that pint1 and point 2 both contain their own copy of a point object, but pen1 and pen2 consist references to the same Pen object?

You can place your cursor over the name of the object type and press F12 tab. This can be easily done in Visual studio. It is important to know that you are dealing with apt type of object or not.

public struct Point { … }     // defines a “value” type

public class Pen { … }        // defines a “reference” type

  • Not understanding or misunderstanding default values for uninitialized variables

Please note- the value types cannot be null in C#. Each value type has its own default value. Even uninitialized variables of value types contain their default value.

  • Implementing unspecified string comparison methods

Many programmers use ‘==’ operator to compare string. This method is one of the least desirable methods used by programmers as it doesn’t specify the comparison type desired for code. Professionals recommend using Equals method to test string equality.

Some tips and guidelines to use equals method:

  • While comparing strings input by user, culture-sensitive comparison should be used (CurrentCulture or CurrentCultureIgnoreCase).

  • You can use ordinal comparison (Ordinal or OrdinalIgnoreCase) while comparing programmatic strings

Note- besides Equals method, you can also avail Compare method that provides you information on relative orders of strings or test for equality.

  • Failing to consider the underlying LINQ statement objects

LINQ is useful and consistent method to query collections of objects, still you need to know whether your statement will be translated to other language other than C# or not, this ensures the code behavior that is expected at runtime.

Some other mistakes are:

  • Avoiding exceptions

  • Eliminating free resources

  • Making use of wrong type of collection for the project

  • Getting puzzled with extension methods

  • Allowing compiler warnings to accumulate

Things NOT to do in ASP.net development

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s