Var (city, population, area) = Quer圜ityData("New York City") The following example uses type inference when deconstructing the three-tuple returned by the Quer圜ityData method. You place the var keyword outside of the parentheses. You can use the var keyword so that C# infers the type of each variable. (string city, int population, double area) = Quer圜ityData("New York City") The following example uses this approach to deconstruct the three-tuple returned by the Quer圜ityData method. You can explicitly declare the type of each field inside parentheses. There are three ways to deconstruct a tuple: For example, the following statement assigns the elements of a four-tuple to four separate variables: var (name, address, city, zip) = contact.GetAddressInfo() The general syntax for deconstructing a tuple is similar to the syntax for defining one: you enclose the variables to which each element is to be assigned in parentheses in the left side of an assignment statement. TuplesĬ# features built-in support for deconstructing tuples, which lets you unpackage all the items in a tuple in a single operation. When you deconstruct an object, you assign selected values to individual variables. To deconstruct a tuple, you assign its elements to individual variables. In C# 7.0 and later, you can retrieve multiple elements from a tuple or retrieve multiple field, property, and computed values from an object in a single deconstruct operation. Retrieving multiple field and property values from an object can be equally cumbersome: you must assign a field or property value to a variable on a member-by-member basis. Private static (string, int, double) Quer圜ityData(string name) Var result = Quer圜ityData("New York City") The Quer圜ityData method returns a three-tuple, and each of its elements is assigned to a variable in a separate operation. Working on an element-by-element basis is cumbersome, as the following example shows. But once you retrieve the tuple, you have to handle its individual elements. A tuple provides a lightweight way to retrieve multiple values from a method call.
0 Comments
Leave a Reply. |