Tuesday, August 12, 2008

Best Practices in VB.NET

Optional parameters

  • Do not use the Optional keyword when defining a public method in a public class.
  • Use overloading to support methods with different numbers of arguments
  • Even though Optional is CLS-compliant, they are not visible to C# clients.
  • For example, if a VB.NET method takes one regular argument and three optional ones, a C# client always must pass four arguments

DirectCast operator

  • Use the DirectCast operator instead of CType/CInt when you unbox a value type, such as convert a object to an Integer.
    • This is inefficient => CInt(object) or CType(object, Integer)
    • Use this => DirectCast(object, Integer)
  • Unlike CType, the DirectCast operator is never translated to a call into the CLR library and is therefore more efficient.
  • The CType operator should be used only to perform conversions - for example, from string to Integer or DateTime.

Reference:

Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C# Developers

blog comments powered by Disqus