I am using C# 8.0 with NullableContextOptions
(Nullable Reference) enabled.
I have a function with this signature:
public static (int line, string content)? GetNextNonEmptyLineDown(this IList<string> contents, int fromLine)
Basically, it returns the line and the content if there is non-empty line down, and returns if there is none.
The problem is I don't know how to deconstruct it. If I try:
var (firstLineIndex, firstLine) = content.GetNextNonEmptyLineDown(0);
I receive the 4 syntax errors:
So I can only use:
var lineInfo = content.GetNextNonEmptyLineDown(0);
var firstLineIndex = lineInfo.Value.line;
var firstLine = lineInfo.Value.content;
which destroys the purpose. lineInfo
is of type struct<T> where T is (int line, string content)
Is there anyway to deconstruct a nullable tuple?
EDIT: after posting the question, it comes to my mind that it makes no sense to allow deconstructing of nullable tuple as the data type of the variables may not be determined. Here is my current fix but wonder if this is the best way:
var lineInfo = content.GetNextNonEmptyLineDown(0);
if (lineInfo == null)
{
throw new Exception();
}
var (firstLineIndex, firstLine) = lineInfo.Value;
content.GetNextNonEmptyLineDown(0);
is null? If you want to swop nulls out for defaults, you can domething like:var lineInfo = content.GetNextNonEmptyLineDown(0) ?? (0, "");
However, that's bad style. So your suggestion is probably best. – Phosphatize