Best pactices in developing asp.net applications - Part 1


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.

3 comments:

  1. One suggestion, instead of checking whether the string is empty or not using string.length() method, we could used string.isnullorempty() method which is already predefined.

    ReplyDelete
  2. One more tip, While converting an object into a String representation, don't use the object.ToString() method, if the object is a null object, then it will throw an exception. Instead try to use the static mrthod "Convert.ToString(object)", this will not throw an exceptiion even if the ibject is null, if the object is null it will return an empty-string.

    ReplyDelete
    Replies
    1. shall we use example
      sqldatareader dr= new sqldatareader();//now this is object formate
      int sid=int dr["sid"];////so we can convert object format into int
      int sname= dr["sname"].tostring();//While converting an object into a String representation,
      plz add more info to my little ans ,

      Delete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us