-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Performance.AvoidRepetitiveCastsRule(2.10)
Sebastien Pouliot edited this page Jan 22, 2011
·
2 revisions
Assembly: Gendarme.Rules.Performance
Version: 2.10
This rule fires if multiple casts are done on the same value, for the same type. Casts are expensive so reducing them, by changing the logic or caching the result, can help performance.
Bad example:
foreach (object o in list) {
// first cast (is)
if (o is ICollection) {
// second cast (as) if item implements ICollection
Process (o as ICollection);
}
}
Good example:
foreach (object o in list) {
// a single cast (as) per item
ICollection c = (o as ICollection);
if (c != null) {
SingleCast (c);
}
}
Bad example:
// first cast (is):
if (o is IDictionary) {
// second cast if item implements IDictionary:
Process ((IDictionary) o);
// first cast (is):
} else if (o is ICollection) {
// second cast if item implements ICollection:
Process ((ICollection) o);
}
Good example:
// a single cast (as) per item
IDictionary dict;
ICollection col;
if ((dict = o as IDictionary) != null) {
SingleCast (dict);
} else if ((col = o as ICollection) != null) {
SingleCast (col);
}
- This rule is available since Gendarme 2.0
Note that this page was autogenerated (3/17/2011 9:31:58 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!