CA1011: Consider passing base types as parameters


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at CA1011: Consider passing base types as parameters.

|Breaking Change|Breaking|

A method declaration includes a formal parameter that is a derived type, and the method calls only members of the base type of the parameter.

When a base type is specified as a parameter in a method declaration, any type that is derived from the base type can be passed as the corresponding argument to the method. When the argument is used inside the method body, the specific method that is executed depends on the type of the argument. If the additional functionality that is provided by the derived type is not required, use of the base type allows wider use of the method.

To fix a violation of this rule, change the type of the parameter to its base type.

It is safe to suppress a warning from this rule

  • if the method requires the specific functionality that is provided by the derived type

    - or -

  • to enforce that only the derived type, or a more derived type, is passed to the method.

In these cases, the code will be more robust because of the strong type checking that is provided by the compiler and runtime.

The following example shows a method, ManipulateFileStream, that can be used only with a FileStream object, which violates this rule. A second method, ManipulateAnyStream, satisfies the rule by replacing the FileStream parameter by using a Stream.

using namespace System;
using namespace System::IO;

namespace DesignLibrary
   public ref class StreamUser
      int anInteger;

      void ManipulateFileStream(FileStream^ stream)
         while((anInteger = stream->ReadByte()) != -1)
            // Do something.

      void ManipulateAnyStream(Stream^ anyStream)
         while((anInteger = anyStream->ReadByte()) != -1)
            // Do something.

using namespace DesignLibrary;

static void main()
   StreamUser^ someStreamUser = gcnew StreamUser();
   FileStream^ testFileStream = 
      gcnew FileStream("test.dat", FileMode::OpenOrCreate);
   MemoryStream^ testMemoryStream = 
      gcnew MemoryStream(gcnew array<Byte>{});

   // Cannot be used with testMemoryStream.



CA1059: Members should not expose certain concrete types