Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

imageFew days ago, in one my earlier post, I discussed about “Some Best Practices for Silverlight Application Development (XAML)” which got a huge hit by my readers. I got several feedbacks on that too. Someone from the community also suggested me to write some best coding practices on C#. This stroke my mind and hence decided to write some best practices about C# coding here from my past few years of experience.

 

For past few years, I learned a lot and tried to share best of them to the others who are new in the software development field. This post I contribute to those fellow members. Hope, I will get more feedbacks and/or suggestions here too.

 

I like C# programming while started with .Net technology. I never tried to write code in VB.Net as I decided my career on that. When I was new to this field, I did lots of mistakes and from those mistakes I learnt more things. There’s a proverb: “Mistakes make you learn more & more…”. My career was one of them. Also there’s a proverb: “There’s no end of learning in life. Each second a person can learn”. So, if there’s any more suggestion, please share that to me. It will help me and others to learn more in proper way.

 

One second, there are various posts available on net regarding the same topic. If you do a Google, you will get a no. of them. But these collections are based on my experience those I learnt and thought to share.

 

Let’s stop here and start discussing the Best Coding Practices of C# application development. Here are some of them:

  • Use proper Naming Conventions
      • Always use Camel or Pascal naming format
      • Avoid all uppercase or lowercase names
      • Never use a name begins with numeric character
      • Always prefer meaningful name for your class, property, method etc.
      • Never build different name varied by capitalization
      • Don’t use the same name used in .Net Framework
      • Avoid adding prefixes or suffixes for your identifiers
      • Always use “I” as prefix for Interfaces
      • Always add “Exception” as suffix for your custom exception class
      • Never prefix or suffix the class name to it’s property name
      • Prefix “Is”, “Has” or “Can” for boolean properties
      • Don’t add prefix for your controls
  • Decide between Value Types and Reference Types
  • Always Use Properties instead of public variables
  • Use Nullable data types whenever require
  • Prefer Runtime Constants over Compile time Constants
  • Prefer “is” and “as” operators while casting
  • Prefer string.Format() or StringBuilder for string concatenation
  • Use Conditional attributes when you need them
  • Use ‘0’ (zero) as default value enum value types
  • Chose between Equals() and Equal (==) operator
  • Always prefer the foreach(…) loop
  • Initialize member variables at the time of assignment
  • Initialize static member variables with static constructors
  • Use constructor chaining whenever possible
  • Minimize boxing & unboxing of objects
  • Properly utilize try/catch/finally blocks
  • Catch only that Exception that you can handle
  • Use IDisposable interface
  • Utilize LazyInitializer in most of the cases
  • Split your logic in several small and simple methods
  • Try to use Patterns & Practices like MVP/MVC/MVVM
  • Always prefer DataBinding to populate values in the UI

These are the points that I wanted to share with you. I already started preparing a big article describing each point in depth with proper example. Will share that to you as soon as possible as a different thread. Till then just follow the points and deliver the best that you can give. Happy Coding.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.