Are common properties that can return acceptable arrays?

According to http://msdn.microsoft.com/en-US/library/0fss9skc(v=vs.120).aspx, properties should not return arrays. How does this interact with generics? If the array is a valid type for a generic type, should I create functions like T GetValue() and SetValue(T value) , not T Value { get; set; } T Value { get; set; } T Value { get; set; } ?

+6
source share
1 answer

This rule is not absolute. Understand where it came from: a property that returns an array must make a copy so that the caller can change its copy and not get into the internal state of the object.

If you are dealing with generic code whose generic code knows nothing about a particular T It does not depend on its internal structure. Therefore, sticking to this rule does not help. Passing arrays to and from shared code is completely acceptable.

Case Study: A List<int[]> .

In addition to this, Servy correctly points out that creating methods to get / set the value does not actually do anything to mitigate the effects described in the reasoning for the rule.

+10
source

Source: https://habr.com/ru/post/975730/


All Articles