Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#

Some Best Practices for C# Application Development

Rate me:
Please Sign up or sign in to vote.
4.34/5 (33 votes)
17 Oct 2010CPOL3 min read 240.5K   44   66
Some C# best coding practices that I learnt over the last few years

image

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

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

I liked C# programming when I 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 made a lot 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 to learning in life. Each second, a person can learn”. So, if there are any more suggestions, please share those with me. It will help me and others to learn more in a proper way.

One second, there are various posts available on the internet regarding the same topic. If you do a Google, you will get a number of them. But these collections are based on my experiences, those I learnt and thought of sharing.

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 which begins with numeric character
    • Always prefer meaningful names for your class, property, method, etc.
    • Never build different names 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 its 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 required
  • 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 which 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

Explanation

You can read the explanation in this article.

History

  • 20th September, 2010 - Initial post
  • 17th October, 2010 - Explanation of points

Explanation of those points are published as an article. Read it here.

 

I would appreciate some more points if you have any to include in this article.


This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 4 Pin
Aamer Alduais8-May-12 19:14
Aamer Alduais8-May-12 19:14 
SuggestionFew Missed out Pin
zenwalker198527-Oct-11 7:05
zenwalker198527-Oct-11 7:05 
Use Path.Combine over "//"
Use String.Concat if not interested in String.Format
Carefully choose params
Always try to use Using(){} block
Try to make sure if you need Dispose pattern
Use static carefully
Never make SqlConnection global/class scope.
Play with CommandBehavior w.r.t Sql operations
Always suffix Delegate and Event respectively for custom delegates and events respectively (as part of naming thingy)
Never use magic strings/numbers instead use resources
prefix private var names with _
Choose carefully between StringBuilder and string (yes you have mentioned it, but some times not always SB is good enough)
Carefully understand the features of out and ref and use it appropriately
Do not use too much of virtual functions (some times it may cause performance overheads since they cant be inlined by JIT
Carefully choose Const vs readonly
Understand how valuetypes and ref types works internally (indepth), perhaps read Eric lippart blog post on same
Carefully understand diff between Linq and foreach. Not always Linq is preferable


P.S: most of the above things have picked up frm my memory and earlier tweets Smile | :)
GeneralRe: Few Missed out Pin
thatraja26-Nov-11 23:01
professionalthatraja26-Nov-11 23:01 
GeneralRe: Few Missed out Pin
zenwalker198527-Nov-11 2:17
zenwalker198527-Nov-11 2:17 
GeneralRe: Few Missed out Pin
Kunal Chowdhury «IN»27-Nov-11 2:35
professionalKunal Chowdhury «IN»27-Nov-11 2:35 
GeneralRe: Few Missed out Pin
zenwalker198527-Nov-11 5:20
zenwalker198527-Nov-11 5:20 
QuestionNot a convention... Pin
lewax0030-Sep-11 10:38
lewax0030-Sep-11 10:38 
GeneralMy vote of 2 Pin
Mukesh.C.Gupta21-Oct-10 23:18
Mukesh.C.Gupta21-Oct-10 23:18 
GeneralSome Best Practices for C# Application Development Pin
ananda_SriLanka19-Oct-10 9:10
ananda_SriLanka19-Oct-10 9:10 
GeneralMy vote of 4 Pin
RajanCRT18-Oct-10 20:08
RajanCRT18-Oct-10 20:08 
GeneralSome Best Practices for C# Application Development Pin
ananda_SriLanka18-Oct-10 19:17
ananda_SriLanka18-Oct-10 19:17 
GeneralRe: Some Best Practices for C# Application Development Pin
Kunal Chowdhury «IN»19-Oct-10 6:22
professionalKunal Chowdhury «IN»19-Oct-10 6:22 
GeneralMy vote of 2 Pin
Bernhard Hofmann18-Oct-10 9:39
Bernhard Hofmann18-Oct-10 9:39 
AnswerRe: My vote of 2 Pin
Kunal Chowdhury «IN»18-Oct-10 18:27
professionalKunal Chowdhury «IN»18-Oct-10 18:27 
GeneralI hate these kind of lists (no offense intended) PinPopular
Jason Christian18-Oct-10 8:10
Jason Christian18-Oct-10 8:10 
AnswerRe: I hate these kind of lists (no offense intended) - Already Published the Explanation Pin
Kunal Chowdhury «IN»18-Oct-10 8:17
professionalKunal Chowdhury «IN»18-Oct-10 8:17 
QuestionPrefer Runtime Constants over Compile time Constants - Why? Pin
bennybechp10-Oct-10 20:45
bennybechp10-Oct-10 20:45 
AnswerRe: Prefer Runtime Constants over Compile time Constants - Why? Pin
Kunal Chowdhury «IN»17-Oct-10 3:38
professionalKunal Chowdhury «IN»17-Oct-10 3:38 
GeneralMy vote of 5 Pin
Narud Shiro5-Oct-10 2:21
Narud Shiro5-Oct-10 2:21 
AnswerRe: My vote of 5 Pin
Kunal Chowdhury «IN»17-Oct-10 3:40
professionalKunal Chowdhury «IN»17-Oct-10 3:40 
GeneralGood advises! Pin
Thornik1-Oct-10 2:00
Thornik1-Oct-10 2:00 
GeneralRe: Good advises! Pin
Kunal Chowdhury «IN»17-Oct-10 3:41
professionalKunal Chowdhury «IN»17-Oct-10 3:41 
GeneralMy vote of 4 Pin
Owen Blacker29-Sep-10 2:31
Owen Blacker29-Sep-10 2:31 
GeneralRe: My vote of 4 Pin
Kunal Chowdhury «IN»29-Sep-10 4:57
professionalKunal Chowdhury «IN»29-Sep-10 4:57 
GeneralRe: My vote of 4 Pin
Kunal Chowdhury «IN»17-Oct-10 3:42
professionalKunal Chowdhury «IN»17-Oct-10 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.