You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ToElementOrDefault<T>, which returns null for an element of another type #121
ToElementOrDefault<T> returns the element of the type T with an id, or null for an id of no element or of an element of another type.
ToElement<T> throws InvalidCastException for an element of another type, the way LINQ First throws for an empty sequence. ToElementOrDefault<T> follows the convention of FirstOrDefault:
In the preceding code, view is null when elementId names a wall, and when it names no element of the document.
ToElement<T> throws for an element of another type, as it does now.
ToElementOrDefault<T> sits beside ToElement<T> in the element id extensions, with the same constraint where T : Element.
Scenario when this would be used?
A tool resolves an id it receives from outside, such as an id a user or another process passes, and treats an id of another element as no match.
Today it writes document.GetElement(elementId) as View in place of the extension.
parameter.AsElementOrDefault<T>() joins the scope: AsElement<T> throws InvalidCastException for an element of another type, the same way ToElement<T> does.
ToElement, ToElement<T>, AsElement, and AsElement<T> throw InvalidOperationException for an id or a parameter that references no element, the way First throws for an empty sequence. ToElementOrDefault and AsElementOrDefault, generic and non-generic, return null for it.
The change is breaking: code that checks the result of ToElement or AsElement for null moves to the OrDefault counterpart.
Activity
Nice3point commentedon Oct 29, 2026
Decision
parameter.AsElementOrDefault<T>()joins the scope:AsElement<T>throwsInvalidCastExceptionfor an element of another type, the same wayToElement<T>does.Nice3point commentedon Oct 29, 2026
Decision
ToElement,ToElement<T>,AsElement, andAsElement<T>throwInvalidOperationExceptionfor an id or a parameter that references no element, the wayFirstthrows for an empty sequence.ToElementOrDefaultandAsElementOrDefault, generic and non-generic, returnnullfor it.The change is breaking: code that checks the result of
ToElementorAsElementfornullmoves to theOrDefaultcounterpart.