CA1819:属性不应返回数组 - 什么是正确的选择?不应、数组、属性、正确

由网友(孤坟站着未亡人)分享简介:我遇到过这样的FxCop规则,并没有真正的内容与如何解决冲突(thread1, 线程2 )。我现在有另一种情况下,我需要改正违法行为的 CA1819 样。I encountered this FxCop rule before and wasn't really content with how to solve v...

我遇到过这样的FxCop规则,并没有真正的内容与如何解决冲突(thread1, 线程2 )。我现在有另一种情况下,我需要改正违法行为的 CA1819 样。

I encountered this FxCop rule before and wasn't really content with how to solve violations (thread1, thread2). I now have another case where I need to correct violations of the CA1819 kind.

具体而言,我有一个算法库,进行一些分析计算的曲线(X,Y)上,与公输入对象是这样的:

Specifically, I have an algorithm-library that performs some analytic calculations on a curve (x,y), with a public "input object" like this:

public class InputObject
{
        public double[] X { get; set; }
        public double[] Y { get; set; }
        // + lots of other things well
}

该对象的X和Y属性被用于数百种内库的位置,通常使用指标。输入物体从不由算法改变,但实际上它不应该若有重要。此外, .Length 通常被称为pretty的。这是一个数学库,以及双[] 是种在那里的标准数据类型。在任何情况下,定影CA1819将需要相当长的一段工作

This object's X and Y properties are used in hundreds of locations within library, typically using indexes. The input object is never altered by the algorithms, but actually it shouldn't matter if so. Also, .Length is called pretty frequently. It's a mathematical library, and double[] is kind of the standard data type in there. In any case, fixing CA1819 will require quite some work.

我想过使用名单,其中,双> ,因为列表支持索引和非常类似于数组,但我不知道这是否会减缓算法还是的FxCop会很乐意与这些列表。

I thought about using List<double>, since Lists support indexing and are quite similar to arrays but I'm not sure whether this may slow down the algorithms or whether FxCop will be happy with those Lists.

什么是替代最好的选择,这些双[] 属性?

What is the best option to replace these double[] properties?

推荐答案

如果它是只读的外部消费者和消费者不希望通过索引来访问它,然后最好是有一个公共的阅读类型的IEnumerable&LT;&GT; 与方法访问添加和删除,这样你就不必阵列暴露给别人乱用

If it is read only to external consumer and consumer does not want to access it by index then the best is to have a public read only property of type IEnumerable<> with method accessors to add and remove, this way you will not have to expose your array to someone to mess with.

如果您需要访问索引,然后将其暴露为只读类型的唯一属性的IList&LT;&GT; ,可能返回只读实例,使用方法添加和删除。

If you need to access the indexers then expose it as read only property of type IList<> and probably return a ReadOnly instance, with methods to add and remove.

这样你保持内部列表的封装,并允许用户访问它在只读方式

This way you keep encapsulation of the internal list and allow consumer to access it in a read only way

阅读全文

相关推荐

最新文章