共用方式為


CA2223:成員不應該只有在傳回型別上不同

型別名稱

MembersShouldDifferByMoreThanReturnType

CheckId

CA2223

分類

Microsoft.Usage

中斷變更

中斷

原因

兩個公用或保護的成員的簽章除了傳回型別以外,其他都相同。

規則描述

雖然 Common Language Runtime 允許使用傳回型別區分其他部分都相同的成員,但這個功能不屬於 Common Language Specification,也不是 .NET 程式語言共通的功能。 當成員間只有傳回型別的差異時,程式開發人員和開發工具將可能無法正確辨別這兩者。

如何修正違規

若要修正此規則的違規情形,請變更成員的設計,使得只需憑藉成員的名稱和參數型別就足以認出該成員,或不公開成員。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

下列以 Microsoft Intermediate Language (MSIL) 撰寫的範例,會顯示違反此規則的型別。 請注意,使用 C# 或 Visual Basic .NET 時,不可違反此規則。

.namespace UsageLibrary
{
  .class public auto ansi beforefieldinit ReturnTypeTest
         extends [mscorlib]System.Object
  {
    .method public hidebysig instance int32
            AMethod(int32 x) cil managed
    {
      // Code size       6 (0x6)
      .maxstack  1
      .locals init (int32 V_0)
      IL_0000:  ldc.i4.0
      IL_0001:  stloc.0
      IL_0002:  br.s       IL_0004

      IL_0004:  ldloc.0
      IL_0005:  ret
    } // end of method ReturnTypeTest::AMethod

    .method public hidebysig instance string
            AMethod(int32 x) cil managed
    {
      // Code size       10 (0xa)
      .maxstack  1
      .locals init (string V_0)
      IL_0000:  ldstr      "test"
      IL_0005:  stloc.0
      IL_0006:  br.s       IL_0008

      IL_0008:  ldloc.0
      IL_0009:  ret
    } // end of method ReturnTypeTest::AMethod

    .method public hidebysig specialname rtspecialname
            instance void  .ctor() cil managed
    {
      // Code size       7 (0x7)
      .maxstack  1
      IL_0000:  ldarg.0
      IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
      IL_0006:  ret
    } // end of method ReturnTypeTest::.ctor

  } // end of class ReturnTypeTest

} // end of namespace UsageLibrary