The following rules outline the usage guidelines for methods:
- Choose a name for your event based on the recommended Method Naming Guidelines.
- Do not use Hungarian notation.
- By default, methods are nonvirtual. Maintain this default in situations where it is not necessary to provide virtual methods. For more information about implementing inheritance, see Base Class Usage Guidelines.
Method Overloading Guidelines
Method overloading occurs when a class contains two methods with the same name, but different signatures. This section provides some guidelines for the use of overloaded methods.
Methods With Variable Numbers of Arguments
You might want to expose a method that takes a variable number of arguments. A classic example is the printf method in the C programming language. For managed class libraries, use the params (ParamArray in Visual Basic) keyword for this construct. For example, use the following code instead of several overloaded methods.
Sub Format(formatString As String, ParamArray args() As Object)
[C#]
void Format(string formatString, params object [] args)
You should not use the VarArgs or ellipsis (...) calling convention exclusively because the Common Language Specification does not support it.
For extremely performance-sensitive code, you might want to provide special code paths for a small number of elements. You should only do this if you are going to special case the entire code path (not just create an array and call the more general method). In such cases, the following pattern is recommended as a balance between performance and the cost of specially cased code.
Sub Format(formatString As String, arg1 As Object)
Sub Format(formatString As String, arg1 As Object, arg2 As Object)
Sub Format(formatString As String, ParamArray args() As Object)
[C#]
void Format(string formatString, object arg1)
void Format(string formatString, object arg1, object arg2)
void Format(string formatString, params object [] args)
See Also
Design Guidelines for Class Library Developers | Method Naming Guidelines | Class Member Usage Guidelines | Base Class Usage Guidelines