In my previous
post, we discussed the possible feature of tuples in C# 7.0. Using the same code, we will discuss another feature named Deconstruction. Thus, I recommend you to go through that feature
first.
In order to retrieve the multiple values returned by the method, we can use this deconstruction feature. In order to do this, we simply need to declare two variables of int and string types to catch the values returned by our method. The syntax will look, as shown below.
- (int age, string dept) = GetEmployeeById(21);
- Console.Write("Age is: " + age + ", Department is:" + dept);
Also, we can use the var keyword for the types, if we do not want to explicitly specify the types of the variables. See the code given below.
- (var age, var dept) = GetEmployeeById(21);
- Console.Write("Age is: " + age + ", Department is:" + dept);
Hope, you enjoyed reading this one. Happy coding.