Some Best Practices for C#

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.

From kunal-chowdhury.com