1.
if (Dictionary<K, V>.TryGetValue(K, out V))
do something with V
instead of
if (Dictionary<K, V>.ContainsKey(K))
do something with Dictionary<K, V>[K]
-
DirectoryInfo.EnumerateFiles();
instead of
DirectoryInfo.GetFiles();
For reference types:
s = o as string;
if (s != null)
proceed
instead of
if (o is string)
s = (string)o;
-
if (counter < X || expensiveFunction())
instead of
if (expensiveFunction() || counter < X)
-
void M<T>(T o) //avoids boxing
{
}
instead of
void M(object o)
{
}
Double lookup
if (Dictionary<K, V>.TryGetValue(K, out V))
do something with V
instead of
if (Dictionary<K, V>.ContainsKey(K))
do something with Dictionary<K, V>[K]
Load 'em all
DirectoryInfo.EnumerateFiles();
instead of
DirectoryInfo.GetFiles();
Two stage casting:
s = o as string;
if (s != null)
proceed
instead of
if (o is string)
s = (string)o;
If the order doesn't matter
if (counter < X || expensiveFunction())
instead of
if (expensiveFunction() || counter < X)
Boxing
void M<T>(T o) //avoids boxing
{
}
instead of
void M(object o)
{
}