Click here to Skip to main content
Click here to Skip to main content

Some Best Practices for C# Application Development

By , 17 Oct 2010
 

imageFew 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 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 to 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

Read them here: http://www.codeproject.com/KB/cs/CSharp_Coding_Practices.aspx

History:

- Initial Post (20th September, 2010)

- Explanation of points (17th October, 2010) 

         Explanation of those points are published as an Article. Read it from here: http://www.codeproject.com/KB/cs/CSharp_Coding_Practices.aspx

Appreciate some more points if you have to include in this article.


License

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

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Member
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4 PinmemberAamer Alduais8 May '12 - 19:14 
SuggestionFew Missed out Pinmemberzenwalker198527 Oct '11 - 7:05 
GeneralRe: Few Missed out Pinmvpthatraja26 Nov '11 - 23:01 
GeneralRe: Few Missed out Pinmemberzenwalker198527 Nov '11 - 2:17 
GeneralRe: Few Missed out Pinmvp_ Kunal Chowdhury _27 Nov '11 - 2:35 
GeneralRe: Few Missed out Pinmemberzenwalker198527 Nov '11 - 5:20 
QuestionNot a convention... Pinmemberlewax0030 Sep '11 - 10:38 
GeneralMy vote of 2 PinmembertheMukesh12321 Oct '10 - 23:18 
GeneralSome Best Practices for C# Application Development Pinmemberananda_SriLanka19 Oct '10 - 9:10 
GeneralMy vote of 4 PinmemberRajanCRT18 Oct '10 - 20:08 
GeneralSome Best Practices for C# Application Development Pinmemberananda_SriLanka18 Oct '10 - 19:17 
GeneralRe: Some Best Practices for C# Application Development PinmentorKunalChowdhury19 Oct '10 - 6:22 
GeneralMy vote of 2 PinmemberBernhard Hofmann18 Oct '10 - 9:39 
AnswerRe: My vote of 2 PinmentorKunalChowdhury18 Oct '10 - 18:27 
GeneralI hate these kind of lists (no offense intended) PinmemberJason Christian18 Oct '10 - 8:10 
AnswerRe: I hate these kind of lists (no offense intended) - Already Published the Explanation PinmentorKunalChowdhury18 Oct '10 - 8:17 
QuestionPrefer Runtime Constants over Compile time Constants - Why? Pinmemberbennybechp10 Oct '10 - 20:45 
AnswerRe: Prefer Runtime Constants over Compile time Constants - Why? PinmentorKunalChowdhury17 Oct '10 - 3:38 
GeneralMy vote of 5 PinmemberAlfredo Blanco5 Oct '10 - 2:21 
AnswerRe: My vote of 5 PinmentorKunalChowdhury17 Oct '10 - 3:40 
GeneralGood advises! PinmemberMember 30824871 Oct '10 - 2:00 
GeneralRe: Good advises! PinmentorKunalChowdhury17 Oct '10 - 3:41 
GeneralMy vote of 4 PinmemberOwen Blacker29 Sep '10 - 2:31 
GeneralRe: My vote of 4 PinmentorKunalChowdhury29 Sep '10 - 4:57 
GeneralRe: My vote of 4 PinmentorKunalChowdhury17 Oct '10 - 3:42 
GeneralString concatenation PinmemberOwen Blacker29 Sep '10 - 2:29 
I think your recommendation is great, but can be optimised. If it's possible to write clear, maintainable code using String.Concat, we should prefer that over String.Format.
 
The optimisation is apparent when you look at the code (in Reflector, for example). String.Format will always end up in this overload:
public static string Format(IFormatProvider provider, string format, params object[] args)
{
    if ((format == null) || (args == null))
    {
        throw new ArgumentNullException((format == null) ? "format" : "args");
    }
    StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
    builder.AppendFormat(provider, format, args);
    return builder.ToString();
}
whereas String.Concat ends up here:
private static unsafe void FillStringChecked(string dest, int destPos, string src)
{
    int length = src.Length;
    if (length > (dest.Length - destPos))
    {
        throw new IndexOutOfRangeException();
    }
    fixed (char* chRef = &dest.m_firstChar)
    {
        fixed (char* chRef2 = &src.m_firstChar)
        {
            wstrcpy(chRef + destPos, chRef2, length);
        }
    }
}
The unsafe string concatenation will always be faster under the hood, if only slightly, than creating a StringBuilder and calling StringBuilder.AppendFormat. And that String.Format always ends up using a StringBuilder also means that we should only use String.Format when it allows clearer code than a StringBuilder.
GeneralRe: String concatenation PinmentorKunalChowdhury29 Sep '10 - 4:55 
GeneralMy vote of 3 PinmemberCody Tang28 Sep '10 - 0:46 
GeneralRe: My vote of 3 PinmentorKunalChowdhury28 Sep '10 - 4:12 
GeneralRe: My vote of 3 PinmemberCody Tang30 Sep '10 - 17:24 
GeneralRe: My vote of 3 PinmentorKunalChowdhury30 Sep '10 - 17:35 
AnswerRe: My vote of 3 PinmentorKunalChowdhury17 Oct '10 - 3:43 
GeneralRe: My vote of 3 Pinmemberzenwalker198527 Oct '11 - 7:08 
GeneralMy vote of 2 PinmemberEmile van Gerwen27 Sep '10 - 21:31 
AnswerRe: My vote of 2 PinmentorKunalChowdhury27 Sep '10 - 22:55 
AnswerRe: My vote of 2 PinmentorKunalChowdhury17 Oct '10 - 3:45 
GeneralMy vote of 2 Pinmemberleidenbach27 Sep '10 - 19:37 
GeneralRe: My vote of 2 PinmentorKunalChowdhury27 Sep '10 - 22:51 
AnswerRe: My vote of 2 PinmentorKunalChowdhury17 Oct '10 - 3:46 
GeneralMy vote of 3 Pinmemberpavan kumar muvvala27 Sep '10 - 18:45 
GeneralRe: My vote of 3 PinmentorKunalChowdhury27 Sep '10 - 22:49 
GeneralRe: My vote of 3 PinmentorKunalChowdhury17 Oct '10 - 3:47 
GeneralMy vote of 3 PinmemberDarchangel27 Sep '10 - 9:33 
AnswerRe: My vote of 3 PinmentorKunalChowdhury27 Sep '10 - 22:48 
GeneralRe: My vote of 3 PinmemberDarchangel28 Sep '10 - 3:38 
GeneralRe: My vote of 3 PinmentorKunalChowdhury28 Sep '10 - 4:10 
GeneralRe: My vote of 3 PinmentorKunalChowdhury17 Oct '10 - 3:49 
GeneralMy vote of 5 PinmemberAbhinav S20 Sep '10 - 5:58 
GeneralRe: My vote of 5 PinmentorKunalChowdhury20 Sep '10 - 6:02 
GeneralRe: My vote of 5 PinmentorKunalChowdhury17 Oct '10 - 3:49 

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 17 Oct 2010
Article Copyright 2010 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid