Improve the guideline for perf consideration (#4824)

* Improve the guideline for perf consideration

* Address comment
This commit is contained in:
Dongbo Wang
2017-09-14 09:24:49 +04:00
committed by Ilya
parent 303a83546b
commit ede580e7d1
+9
View File
@@ -123,6 +123,15 @@ Some general guidelines:
Instead, use `var example = obj as Example` or the C# 7 syntax `if (obj is Example example) {...}` as appropriate.
In this way you can avoid converting to the type twice.
* Use generic collections instead of the non-generic ones such as `ArrayList` and `Hashtable` to avoid type casting and unnecessary boxing whenever possible.
* Use collection constructor overloads that take an initial capacity for collection types that have them.
Internally, `List<T>`, `Dictionary<TKey, TValue>`,
and the other generic collections use one or more arrays to hold valid data.
Whenever resizing is needed,
one or more new arrays double the size of existing arrays are created and items from the existing arrays are copied.
Setting an approximate initial capacity will reduce the number of resizing operations.
* Use `dict.TryGetValue` instead of `dict.Contains` and `dict[..]` when retrieving value from a `Dictionary`.
In this way you can avoid hashing the key twice.