Skip to content

Instantly share code, notes, and snippets.

@controlflow
Last active September 3, 2016 14:33
Show Gist options
  • Save controlflow/6b90e498ce07ffb193f720eed1607ae9 to your computer and use it in GitHub Desktop.
Save controlflow/6b90e498ce07ffb193f720eed1607ae9 to your computer and use it in GitHub Desktop.
var x = e as T;
if (x != null) {
...
} else {
// no usages of 'x' here
}
// no usages of 'x' here too
// =>
if (e is T x) {
...
}
/////////////////////////
if (x != null && x is T t)
// =>
if (x is T t)
//////////////////////////////////
if (x is T && x is T t)
// =>
if (x is T t)
//////////////////////////////////
if (x is T t && ((T) x).P != null)
// => NOT - can't be merged with (try)cast if 'is' introducing name
if ((x as T).P != null
//////////////////////////////////
if (x is T t && t.P != null)
// => BUT only if 't' is not used + first looks better!
if ((x as T)?.P != null)
/////////////////////////////
if (x == null) {
return e;
}
return x; // or 'x.NotNullMember();'
// or
if (x != null) {
return x; // or 'x.NotNullMember();'
}
return e;
// =>
return x ?? e;
////////////////////////////////////
T x = null;
if (e != null) x = e.P;
// =>
T x = e?.P;
// or
T x = e != null ? e.P : null; // check can derive type
////////////////////////////////////
if ((T as x)?.P is U) {
var p = (U) ((T)x).P;
}
// =>
if ((T as x)?.P is U u) {
var p = u;
}
////////////////////////////////////
while (index < statements.Count && statements[index] is ISwitchLabelStatement)
cases.Add((ISwitchLabelStatement) statements[index]);
while (index < statements.Count && statements[index] is ISwitchLabelStatement sw)
cases.Add(sw);
///////////////////////////////////
var xs = new List<FooNode>();
for (var node = x; node is FooNode; node = ((FooNode) node).Parent) {
xs.Add((FooNode) node);
}
for (var node = x; node is FooNode foo; node = foo.Parent) {
xs.Add(foo);
}
///////////////////////////////////
CA "Null propagation to 'if' statement"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment