Monday, 9 October 2017

C# 7 value tuples and enumerables

Today I found myself fighting with Visual Studio about how to check if the result of a FirstOrDefault operation on a list of a value type was null. Tried all the following and nothing worked, even with several results from stack overflow.
I created a quick test harness to try to find a "solution" to my problem:

My first reflex was to check if it was null as we usually do with generic lists of non value types.


Then I realized that I was so used to work with enumerables of reference types that I had completely overlooked the name of this new types  value tuple, so I tried to check for defaults.So I tried with default, default of value type and default of tuple, none of them work.



For this last one I also switched from C# 7 to 7.1, but the compiler continued complaining.


After a bit of google I noticed that I was not the only one with this kind of problem as it seems that the new value tuples don't play well with enumerable just yet. So I went back to use reference types for this case as the solution I found is far from elegant and error prone:


No comments:

Post a Comment