evan :
I see the following code sometimes, and have no idea what the expression is actually testing.
public static void Something(string[] value)
{
if (value is { })
{
DoSomethingElse();
}
}
Daniel A. White :
That's just the empty property pattern in C# 8, meaning the value not null
. It matches any value type or reference type. As Panagiotis Kanavos notes in the comments, this is equivalent to the good old value is object
check which has been in C# for a long time.
Generally if you were to specify a property, then it would match or not. This esoteric example illustrates that:
if (value is { Length: 2 })
{
// matches any object that isn't `null` and has a property set to a length of 2
}
The property patterns work best and are most clear when comparing with other patterns in cases such as switch
expressions.