Another concept, derived from functional programming languages, joins the camp of C # and VB, which is called pattern-matching (Matching) features. At first glance, pattern matching acts like a block of switch/select statements, but it functions much more powerfully.
Note: Since the VB version specification is not yet complete, these examples are mostly from pattern-matching specifications in C #.
is or matches operator
. The pattern matching in net is mainly supported by the "Is/matches" operator. This is also a less familiar operator that decomposes a class into multiple components. The following example is based on the Cartesian record class Http://www.infoq.com/news/2014/08/Record-Class (record classes) that was mentioned in the Tuesday news report.
public static bool operator are (Cartesian C, out Double X, out double y) x = c.x; y = c.y; return true;}
This is operator is not limited to its definition class, the following example defines an operator in another way, allowing it to decompose a Cartesian object so that it can match a polar object.
public static class Polar {public static bool operator are (Cartesian C, out double R, out double Theta) { r = MATH.SQRT (c.x*c.x + c.y*c.y); Theta = Math.atan2 (c.y, c.x); return c.x! = 0 | | C.y! = 0; }} var c = Cartesian (3, 4); if (c is Polar (Var r, *)) Console.WriteLine (R);
Type mode
The simplest pattern is the type pattern, which essentially attempts to type-convert and assign to a variable at the same time. The following is an example of this pattern:
if (expr is Type v) {//code using V}
Recursive mode
Most patterns appear as recursive patterns, meaning that they are made up of simpler patterns. Take a look at an example of this pattern:
var a = new location (1, 2, 3); X=1, y=2, Z=3if (A is location (1, var y, *))
This recursive pattern includes a constant pattern, a var pattern, and a wildcard pattern.
Constant mode
This pattern can match a property to a constant value, and the constants match using object. Equals (left, right) method to determine if the two matches.
var mode
The Var pattern is bound to succeed, and the variable that corresponds to the pattern is assigned the value provided when the IS operator is invoked, and the type of the variable is the static type determined by the expression.
Wildcard mode
The wildcard pattern is essentially a var expression, but you don't need to worry about matching results.
Internal implementation
Let's go on to the example of this location class, where the compiler will handle the following steps during the compile phase:
Create variables $x, $y, and $z
Call Location.is (A, out $x, out $y, out $z) method, and confirm that the return result is true
Constant mode: Check for object. The result of the Equals ($x, 1) call
var mode: Assign $y to Y
Wildcard mode: Ignore $z variable
Switch/select Case Statement Block
The function of the switch statement block will be extended, and it will be able to use pattern matching attributes. This actually means that you can write a statement in the following way:
Case Null:case String scase location (1, var y, *):
Limit
In the current specification draft, there is no support for scope checking. This means that you cannot write code similar to "A was location (> 0, 1 to 5, <= 10)". Element matching is also not supported in a list or iterator at this time.