.NET Coding Best Practices






2.58/5 (27 votes)
.NET Coding Best Practices - Vinayak's thumb rulesDo not hard code strings/ numerics. Instead of that use constants as shown below.Bad practice int Count; Count = 100; if( Count == 0 ) { // DO something… }Good practice int Count; Count = 100; private...
.NET Coding Best Practices - Vinayak's thumb rules
- Do not hard code strings/ numerics. Instead of that use constants as shown below.
Bad practice
int Count; Count = 100; if( Count == 0 ) { // DO something… }
Good practiceint Count; Count = 100; private static const int ZERO = 0; if( Count == ZERO ) { // DO something… }
- For string comparison - Use String. Empty instead of “”
- By default keep the scope of member variables to ‘private’, based on the needs expand the scope either to protected or public or internal.
- Other advantage of making the scope private by default is that during XMLSerilaization, by default it will serialize all the public members.
- When we have to manipulate the strings inside a loop, use StringBuilder instead of string as shown below.
Bad practice
String temp = String.Empty; for( int I = 0 ; I<= 100; i++) { Temp += i.ToString(); }
Good practiceStringBuilder sb = new StringBuilder(); for ( int I = 0 ; I<= 100; i++) { sb.Append(i.ToString()); }
- Prefer Arrays over Collections for simple operations.
- Prefer Generic Collections over ArrayList
- Prefer Generic Dictionaries over HashTable *.
- Prefer StringCollections and StringDictionaries for String manipulations and storage.
- Use the appropriate data types.
For ex: if you want to check for any status, prefer bool rather than int.
Bad practice
int Check = 0; if(Check == 0) { // DO something }
Good practicebool Check = false; if(!Check) { // DO something }
- Use ‘as’ operator for type casting and before using that resultant value check for null.
class A { } class B : A { } B objB = new B(); A objA1 = (A) objB; A objA 2 = objB as A; if( objA2 != null) { //Do something }
- For WCF proxy creation, use the using statement
using(Cerate the proxy) { //Do the required operation }
- Follow ‘Acquire late, release early’ rule for expensive resources such as Connection, File etc.
For ex : if you want to make use of SqlConnection Object for any data operations, create the instance at the method level rather than at the class level.
class MyData { public MyData() { } public List
If you want to create the SqlConnection instance at the class level, ensure that you are implementing the IDisposable for the class and doing the cleanup of SqlConnection instance at the Dispose();GetAllCustomer() { using(SqlConnection objConnection = new SqlConnection(“Connection string”)) { //Do the operation and get the required data.. } } } class MyData : IDisposable { SqlConnection objConnection = default(SqlCOnnection); public MyData(){ objConnection = new SqlCOnnection(“Connection string”); } public List
GetAllCustomer(){ // By using objConnection get the required data } public void Dispose(){ // Do the cleanup of SqlCOnnection if( objConnection != null ){ if( objConnection.State == ConnectionState.Open){ objConnection.Close(); } } } } - If you do not want anybody to extend your class functionalities, make it ‘sealed’ to get the inlining and compile time benefits
- Avoid declaring the ‘destructor’ for every class. This will increase the life time of the class which un necessarily makes them long lived
- Prefer using Thread Pool over manual threading.
- Do not make any calls to a method from the loop.
For ex :
Bad practice
for( int i = 0; i<= 100; i++) { Calculate(i); }
Good practicefor( int i = 0; i<= 100; i++) { //Inline the body of Calculate. }
- Do not handle exceptions inside a loop, rather handle looping logic inside try/catch
For ex:
Bad practice
for(int i = 0 ; i<= 100; i++){ try{ } catch(Exception ex){ throw ex; } }
Good practicetry{ for(int i = 0 ; i<= 100; i++){ } } catch(Exception ex){ throw ex; }
- Do not handle application logic by using Exception.
For ex :
Bad practice
try{ int x,y,z; x = 0; y = 10; z = y/x; } catch(DevideByZeroException ex){ throw ex; }
Good practiceprivate static const int ZERO = 0; try{ int x,y,z; x = 0; y = 10; if( x != ZERO){ z = y/x; } } catch(Exception ex){ }
- Prefer for/while loop over foreach
- For communication between the layers, prefer Data Transfer objects over DataSet/DataTables.