Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the benefit of var patterns in C#7?

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 as expr.

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?

like image 390
Tim Schmelter Avatar asked Feb 23 '18 11:02

Tim Schmelter


People also ask

What is the use of var keyword?

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.

Why should I use var in C#?

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.

What is var in C sharp?

var is a keyword, it is used to declare an implicit type variable, that specifies the type of a variable based on initial value.

Is VAR reference type?

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.


1 Answers

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}) { … } 
like image 171
poke Avatar answered Sep 19 '22 23:09

poke