Click here to Skip to main content
13,862,316 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


32 bookmarked
Posted 13 Jan 2011
Licenced CPOL

Best practices in developing ASP.NET applications

, 19 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Best practices in developing ASP.NET applications
Best practices in developing ASP.NET applications...

  1. Remove unused private fields and functions.
  2. Do not cast unnecessarily. Avoid duplicate casts where possible, since there is a cost associated with them.
  3. Properties that return arrays are prone to code inefficiencies. Consider using a collection or making this a method.
  4. To test for empty strings, check if String.Length is equal to zero. Constructs such as "".Equals(someString) and String.Empty.Equals(someString) are less efficient than testing the string length. Replace these with checks for someString.Length == 0.
  5. Methods in the same type that differ only by return type can be difficult for developers and tools to properly recognize. When extending a type, be sure not to define new methods that differ from base type methods only by type.
  6. Use stringbuilder instead of string types for string manipulation.
  7. Use String.Format instead of concatenating and appending strings.
  8. Use Type.TryParse rather than Convert.ToDestinationType(). For example, use int.TryParse() rather than Convert.ToInt32() which might throw an exception.
  9. Override Equals() method wherever applicable in your classes.
  10. Consider passing base types as parameters - Using base types as parameters to methods improves re-use of these methods if you only use methods & properties from the parameter's base class. E.g. use Stream instead of FileStream as a parameter when only calling Stream.Read(), this makes the method work on all kind of streams instead of just File streams.
  11. Do not catch general exception types - You should not catch Exception or SystemException. Catching generic exception types can hide run-time problems from the library user, and can complicate debugging. You should catch only those exceptions that you can handle gracefully.
  12. Use properties instead of visible instance fields.
  13. Follow the same naming conventions accross the solution.
  14. Remove unwanted commented code, indent code properly.
  15. Use curly braces with in an if statement, even if there is a single statement in the if block. This will provide better readability.
  16. Make sure to refactor your code to move the duplicated code to common reusable functions.
  17. Move one time control settings into the .aspx page rather than having them in the code behind in if(!IsPostback) block.
  18. Use inheritance wherever possible, which enables code reuse and also reduces the amount of code we have to write and test.
  19. Move the reusable JavaScript functions to an external .js file instead of having them on the page.
  20. For controls that are declaratively specified on the page, tie the event handlers to the controls events on the aspx page rather than initializing them in the codebehind. If the controls are built dynamically, then we do not have a choice.
  21. Make sure to check for nulls when using any type retrieved from a session, querystring or a database to avoid NullReferenceExceptions.
  22. Use foreach loop instead of using for loop which may lead to out of boundary run time exceptions.


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


About the Author

Mahendra Vishwakarma
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 good one!!!! Pin
sagar viradiya2-Feb-12 0:49
membersagar viradiya2-Feb-12 0:49 
GeneralReason for my vote of 4 Great article Pin
kulkarni.ajay14-Feb-11 17:37
memberkulkarni.ajay14-Feb-11 17:37 
GeneralReason for my vote of 5 Good summary of tips for beginner pr... Pin
DuffmanLight7-Feb-11 4:22
memberDuffmanLight7-Feb-11 4:22 
GeneralThis is rather a confused list - most of the points are abou... Pin
Member 25604727-Feb-11 3:02
memberMember 25604727-Feb-11 3:02 
GeneralReason for my vote of 1 Not as described by title. Mostly a ... Pin
Member 25604727-Feb-11 2:13
memberMember 25604727-Feb-11 2:13 
GeneralReason for my vote of 5 nice one Pin
Pranay Rana2-Feb-11 1:03
professionalPranay Rana2-Feb-11 1:03 
GeneralReason My vote of 5 all point is sounds like great Pin
RaviRanjanKr1-Feb-11 1:01
professionalRaviRanjanKr1-Feb-11 1:01 
GeneralReason for my vote of 5 Nice Tricks! My vote of 5 Pin
RaviRanjanKr1-Feb-11 1:00
professionalRaviRanjanKr1-Feb-11 1:00 
GeneralReason for my vote of 4 I like the points. Pin
sendtodilip24-Jan-11 19:43
professionalsendtodilip24-Jan-11 19:43 
General@Murrali...thanks for suggestion, I'll try to do it asap... Pin
Mahendra Vishwakarma18-Jan-11 0:09
memberMahendra Vishwakarma18-Jan-11 0:09 
GeneralRe: Hi, correct the typo from the title. 'pactices' instead of '... Pin
thatraja18-Jan-11 6:03
protectorthatraja18-Jan-11 6:03 
General@Chris Hey....thanks for such comment... Here we see how you... Pin
Mahendra Vishwakarma17-Jan-11 23:59
memberMahendra Vishwakarma17-Jan-11 23:59 
GeneralRe: but if string is null then the check on string.length will g... Pin
Sumit_Gupta7-Feb-11 18:32
memberSumit_Gupta7-Feb-11 18:32 
GeneralThanks for this post. But it will be great and useful if we ... Pin
Murrali from Madras17-Jan-11 23:40
memberMurrali from Madras17-Jan-11 23:40 
General"To test for empty strings, check if String.Length is equal ... Pin
Chris Hey17-Jan-11 23:01
memberChris Hey17-Jan-11 23:01 
GeneralReason for my vote of 2 Seems to be code review comments fro... Pin
Bheemareddy Srinivas17-Jan-11 2:29
memberBheemareddy Srinivas17-Jan-11 2:29 
GeneralReason for my vote of 5 thanks for sharing Pin
Pranay Rana17-Jan-11 1:41
professionalPranay Rana17-Jan-11 1:41 
GeneralThis is already posted in several other places. Do we really... Pin
Indivara13-Jan-11 22:50
professionalIndivara13-Jan-11 22:50 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 19 Jan 2011
Article Copyright 2011 by Mahendra Vishwakarma
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid