Welcome to Geekdojo Sign in | Join | Help

Don'ts in API design (FxCop shows me some Don'ts)

NOT COOL :-
1) Properties Should Not Return Arrays
Why ??
.NET Framework SDK Documentation :-
You should use collections to avoid code inefficiencies. In the following code example, each call to the myObj property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop.

[C#]
for (int i = 0; i < obj.myObj.Count; i++)
   DoSomething(obj.myObj[i]);

2) Properties that return collections should be read-only
Why ??
FxCop - Properties that return collections should be read-only so that users cannot entirely replace the backing store. Users can still modify the contents of the collection by calling relevant methods on the collection.

I was given these lessons today by Microsoft FxCop.

Published Wednesday, November 19, 2003 1:38 PM by richardhsu
Filed Under:

Comments

Anonymous comments are disabled