Home > Articles

  • Print
  • + Share This

Joe's Tips

  1. When desiring cross-language compliance, implement methods where common operators are spelled out. This way, other .NET languages that can’t overload these operators can still implement the same type of operation, that is, define an Equals() method in addition to the = operator, define an Add() method in addition to the + operator, and so on.

  2. Encapsulate fields with properties. This way the internal implementation of how the field is used and even the field’s type can change without affecting the user. Another benefit is that the user can use the property just like a field and the user won’t know the difference.

  3. Expose public class members through a formal interface and restrict access of non-interface members with the private, protected, and internal modifiers. interfaces can be implemented on other classes, which promotes code reuse.

  4. Overloading constructors enables a class to expose optional constructor parameters. That is, if you declare two constructors for a class, MyClass

    	 public MyClass(int p1) { }
    
         public MyClass(int p1, int p2) { }
    
    

    a user may optionally instantiate MyClass with either one or two parameters.

  5. Implementing an ArrayList collection allows array-like semantics with dynamic resizing capability. For example, an array declared with a size of five will throw an ArrayOutOfBoundsException exception when an attempt to add a sixth item is made. However, an ArrayList collection will grow as necessary to accommodate as many items as your hardware and operating system resources can support.

  6. Implement the [assembly: CLSCompliant(true)] attribute to find non-CLS-compliant code when creating an assembly that will be used by other .NET languages. This attribute will cause a compile time error to be generated anytime a program contains non-CLS-compliant code.

  7. The alias directive will permit a large namespace to be renamed, which can help make code more readable. You make the choice of which is easier to read:

    	using SoapFmt = System.Runtime.Serialization.Formatters.Soap; 
    
    
    
    SoapFmt or System.Runtime.Serialization.Formatters.Soap.
    	using SoapFmt = System.Runtime.Serialization.Formatters.Soap; 
    
    
    
    SoapFmt or System.Runtime.Serialization.Formatters.Soap.
  8. abstract classes help create inheritance hierarchies with default implementations. interfaces establish public contracts, which a class or struct must implement. Use abstract classes when derived classes can take advantage of the default implementation. The object class is the perfect example of an abstract class. When you need to force a class to expose specific public methods, use one or more interfaces.

  9. try/catch/finally blocks can be effective anytime there is the possibility of an exception being thrown. Any time you perform a file I/O, network communication, or database operation, you need to guard against exceptions that are likely to be thrown.

  10. To achieve the greatest flexibility while overloading methods, implement integral value parameters as long and floating-point values as double. Because of implicit conversion, calls to such methods with smaller type arguments will be possible. The following method accepts a short:

    void MyMethod(short) { }

    The problem with MyMethod() is that a caller can’t pass an int. However, if the method were defined like this

     void MyMethod(long) { }

    then the method could accept int, byte, sbyte, short, ushort, and ulong arguments.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.