Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Development Edition
Design Warnings
 Default parameters should not be us...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Team System
Default parameters should not be used

TypeName

DefaultParametersShouldNotBeUsed

CheckId

CA1026

Category

Microsoft.Design

Breaking Change

Breaking

An externally visible type contains an externally visible method that uses a default parameter.

Methods that use default parameters are allowed under the Common Language Specification (CLS); however, the CLS allows compilers to ignore the values assigned to these parameters. Code written for compilers that ignore default parameter values must explicitly provide arguments for each default parameter. To maintain the behavior that you want across programming languages, methods that use default parameters should be replaced with method overloads that provide the default parameters.

The compiler ignores the values of default parameters for C# and Managed Extension for C++ when accessing managed code. The Visual Basic compiler supports methods with default parameters using the Optional (Visual Basic) keyword.

To fix a violation of this rule, replace the method that uses default parameters with method overloads that supply the default parameters.

Do not suppress a warning from this rule.

The following example shows a method that uses default parameters, and the overloaded methods that provide an equivalent functionality.

Visual Basic
Imports System

<Assembly: CLSCompliant(True)>
Namespace DesignLibrary

    Public Class DefaultVersusOverloaded

        Sub DefaultParameters(Optional parameter1 As Integer = 1, _
                              Optional parameter2 As Integer = 5)
            ' ...
            Console.WriteLine("{0} : {1}", parameter1, parameter2)
        End Sub

        Sub OverloadedMethod()
            OverloadedMethod(1, 5)
        End Sub

        Sub OverloadedMethod(parameter1 As Integer)
            OverloadedMethod(parameter1, 5)
        End Sub

        Sub OverloadedMethod(parameter1 As Integer, parameter2 As Integer)
            ' ...
            Console.WriteLine("{0} : {1}", parameter1, parameter2)
        End Sub

    End Class

End Namespace

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker