[FxCop.设计规则]7. 集合类应该实现泛型接口

7.     集合类应该实现泛型接口

原文引用:

Collections should implement generic interface

TypeName:

CollectionsShouldImplementGenericInterface

CheckId:

CA1010

Category:

Microsoft.Design

Message Level:

Error

Certainty:

90%

Breaking Change:

NonBreaking


Cause: An externally visible type implements the System.Collections.IEnumerable interface but does not implement the System.Collections.Generic.IEnumerable<T> interface, and the containing assembly targets .NET Framework version 2.0. This rule ignores types that implement System.Collections.IDictionary.

Rule Description

To broaden the usability of a collection, implement one of the generic collection interfaces. Then the collection can be used to populate generic collection types such as the following:

How to Fix Violations

To fix a violation of this rule, implement one of the following generic collection interfaces:

When to Exclude Messages

It is safe to exclude a message from this rule; however, the collection will have a more limited use.

Example Code

The following example shows a collection, IntegerCollection, that violates the rule. In GenericIntegerCollection, the collection is modified to satisfy the rule by implementing System.Collections.Generic.IEnumerable<T>. Finally, the collection is used to populate a strongly typed stack.

[C#]

using System;

using System.Collections;

using System.Collections.Generic;

 

namespace DesignLibrary

{

   // This class violates the rule.

   public class IntegerCollection: CollectionBase

   {

      // Methods overrides using Int32.

   }

 

   // This class satisfies the rule.

   public class GenericIntegerCollection:

      CollectionBase, IEnumerable<int>

   {

      public int Add(int value)

      {

         return InnerList.Add(value);

      }

 

      // Other method overrides using Int32.

 

      public new IEnumerator<int> GetEnumerator()

      {

         foreach (int data in InnerList)

         {

            yield return data;

         }

      }

   }

 

   class Test

   {

      static void Main ()

      {

         IntegerCollection intCollection = new IntegerCollection();

 

         // The following line would generate a compiler error.

         //Stack<int> integerStack = new Stack<int>(intCollection);

 

         GenericIntegerCollection genericIntCollection =

            new GenericIntegerCollection();

         genericIntCollection.Add(2);

         genericIntCollection.Add(4);

 

         Stack<int> integerStack = new Stack<int>(genericIntCollection);

         Console.WriteLine(integerStack.Pop());

         Console.WriteLine(integerStack.Pop());

      }

   }

}

Related Rules

Avoid excessive parameters on generic types

Do not declare static members on generic types

Do not expose generic lists

Do not nest generic types in member signatures

Generic methods should provide type parameter

Use generic event handler instances

Use generics where appropriate

See Also

Generics DesignGuidelines

引起的原因:

一个使用.NET Framework2.0的程序集有一个输出类型实现了System.Collections.IEnumerable接口,但是没有实现System.Collections.Genceric.IEnumerable<T>接口。如果这个类型实现了System.Collections.IDictionary接口,将不被这个规则所检察。

描述:

为了提升一个集合的可用性,应该实现范型集合接口中的一个。这样,这个集合将能够被用来声明下面的范型类。

修复:

实现下面的范型集合接口中的一个。

例外:

不符合这条规则是安全的,但是,这样将会使你的集合类有更多的使用限制。

译注:

实现至少一个泛型集合接口使你的类型能够被用来构造下面的范型集合类。

例如在原文的例子中的GenericIntegerCollection对象就可以被用来构造一个Stack<int>对象,但是如果你不实现这些接口,构造Stack<int>对象将会产生一个编译警告。实现范型集合接口将会使你的使用者拥有更多的灵活性去使用你的集合类。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值