-
The code below compiles successfully although it doesn't look like any of the documented kinds of pattern matching, also why does the compiler requires a destructor for the I expected that this code will simply extract the value of the constant expression class Program
{
static void Main()
{
Test(new Foo());
}
static void Test(Foo o)
{
const int i = 1;
if (o is ((double)i) f)
{
}
}
class Foo
{
public void Deconstruct(out double d)
{
d = 1;
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So, the confusing part here is The only subpattern of the positional pattern is the constant pattern Though it does look confusing, so I wouldn't suggest you actually write such code and I can't think of a situation when you would want to anyway. |
Beta Was this translation helpful? Give feedback.
So, the confusing part here is
((double)i)
. The language understand this as a positional pattern, which can be used to deconstruct tuples, or other types that have theDecontruct
method. A more common use of such pattern would be something likevar tuple = (1, 2); if (tuple is (3, 4)) ...
.The only subpattern of the positional pattern is the constant pattern
(double)i
. A more common use of a constant pattern would bedouble x = 1.0; if (x is 0.0) ...
.Though it does look confusing, so I wouldn't suggest you actually write such code and I can't think of a situation when you would want to anyway.