I don't understand the use case of var
patterns in C#7. MSDN:
A pattern match with the
var
pattern always succeeds. Its syntax is
expr is var varname
where the value of expr is always assigned to a local variable named
varname
.varname
is a static variable of the same type asexpr
.
The example on MSDN is pretty useless in my opinion, especially because the if
is redundant:
object[] items = { new Book("The Tempest"), new Person("John") }; foreach (var item in items) { if (item is var obj) Console.WriteLine($"Type: {obj.GetType().Name}, Value: {obj}"); }
Here i don't see any benefits, you could have the same if you access the loop variable item
directly which is also of type Object
. The if
is confusing as well because it's never false
.
I could use var otherItem = item
or use item
diectly. Can someone explain the use case better?
The var keyword is used to declare variables in JavaScript. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization.
It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types. The complaint that var reduces readability is not shared by everyone.
var is a keyword, it is used to declare an implicit type variable, that specifies the type of a variable based on initial value.
Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.
The var pattern was very frequently discussed in the C# language repository given that it’s not perfectly clear what its use case is and given the fact that is var x
does not perform a null check while is T x
does, making it appear rather useless.
However, it is actually not meant to be used as obj is var x
. It is meant to be used when the left hand side is not a variable on its own.
Here are some examples from the specification. They all use features that are not in C# yet but this just shows that the introduction of the var pattern was primarly made in preparation for those things, so they won’t have to touch it again later.
The following example declares a function Deriv
to construct the derivative of a function using structural pattern matching on an expression tree:
Expr Deriv(Expr e) { switch (e) { // … case Const(_): return Const(0); case Add(var Left, var Right): return Add(Deriv(Left), Deriv(Right)); // … }
Here, the var
pattern can be used inside the structures to “pull out” elements from the structure. Similarly, the following example simplifies an expression:
Expr Simplify(Expr e) { switch (e) { case Mult(Const(0), _): return Const(0); // … case Add(Const(0), var x): return Simplify(x); } }
As gafter writes here, the idea is also to have property pattern matching, allowing the following:
if (o is Point {X is 3, Y is var y}) { … }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With