Maybe this question has been answered before, but the word if occurs so often it\'s hard to find it.
The example doesn\'t make sense (the expression is
C# 7.0 introduced ability to declare out variables right inside conditions. In combination with generics, this can be leveraged for the requested result:
public static bool make (out T result) where T : new() {
result = new T();
return true;
}
// ... and later:
if (otherCondition && make(out var sb)) {
sb.Append("hello!");
// ...
}
You can also avoid generics and opt for a helper method instead:
public static bool makeStringBuilder(out StringBuilder result, string init) {
result = new StringBuilder(init);
return true;
}
// ... and later:
if (otherCondition && makeStringBuilder(out var sb, "hi!")) {
sb.Append("hello!");
// ...
}