You can combine them.
return GetHeaders<THeader>(name)?.FirstOrDefault() ?? default;
But note that this is changing the expression ever so slightly. This expression says to call the GetHeaders
method and if it doesn't return null then get the first item. Then if the first item is not null return it otherwise return default(T).
In your original code you are calling GetHeaders
. If the value is not null then you get the first item and return it (whether it is null or not). However if GetHeaders
returns null then you return default(T). Note that I assume that method is returning IEnumerable<T>
and therefore should never return null. In pretty much every implementation (including if you're using the iterator syntax) it will return an empty enumerable and never null.
If you're getting an error on the above expression I gave then please tell us what the actual error is.