Tuples C# 7

by

In order to return multiple values from a method in C#, we can use approaches like creating out parameters, creating a custom class with the required properties, or using tuples. 

 

However, in C# 7.0, we have the concept of tuples (quite similar to existing tuples), which can help in returning multiple values from a method.

 

Suppose we have a method, which will return the Department and Age of an employee, based on the EmployeeID. Using the old ways, we could have two out parameters for department and age, along with the EmployeeID, or create a custom class of an employee type and return the values or even use the existing tuple types. However, with new features, we can have a method with two return type values; i.e., int age and string department. The signature of the method would look, as shown below.

public static (int, string) GetEmployeeById(int employeeId)         {             int empAge = 32;             string dept = "HR";             return (empAge, dept);         }

After calling the method, we can access the return values, as shown below.

var employeeDetails = GetEmployeeById(21);  Console.Write("Age is: " + employeeDetails.Item1+ ", Department is:" + employeeDetails.Item2); 

The code does not seem to be too user friendly here, as we have to use the keywords Item1, Item2 etc. (similar to the existing tuples concept). However, we can change our method signature to include the use of names for the return values. Hence, the code will change to what is shown below.

public static (int age, string department) GetEmployeeById(int employeeId)           {               int empAge = 32;               string dept = "HR";               return (empAge, dept);           }  

Now, in order to access the elements, our code will change to what is shown below.

var employeeDetails = GetEmployeeById(21);    Console.Write("Age is: " + employeeDetails.age + ", Department is:" + employeeDetails.department);

this was about the concept of tuples in C# 7.0.

 

 

?Source :C#Corner,MSDN

C# 7 Local Functions

by

The concept of function within a function, like we normally do inside a class.

We will create a method GetName() .

The code will look like the following.

static void Main(string[] args)  {     Console.WriteLine("Name is: " + GetName());  }  public static string GetName()  {    return "Name";  }  

Next, we need to add more , will create Function called GetFullName() , will call 2 methods GetFName() and GetLName. 

This result will be added to the GetFullName() and the complete result will be returned.

 

The main point here is that if we create a public method to concate our string togather, it will be globally available. If we create a private method, the method will be available to other methods as well, which is not required. So, what we will do is create the method as a sub-method within the GetFullName function. This will be created like any other normal function. So, the code will look like the following.

namespace ConsoleApplication1  {      class Program      {          static void Main(string[] args)          {              Console.WriteLine("Full Name is: " + GetFullName());          }          public static string GetFullName()          {              var FullName= GetFName() + " " +GetLName();                return FullName;                          string GetFName()              {                  return "First Name";              }              string GetLName()              {                  return "Last Name";              }          }      }  

 we have created another two methods GetFName() and  GetLName() within a function to return FullName().