Dot Net For All

C# 7.0 Features With Simple Examples

C# 7.0

In this article I will discuss about the C# 7.0 features with simple examples. The article also include the comparisons with the previous version.

For Trying C# 7.0 you need to do the following

C# 7.0 Features

  1. Tuple – I have discussed about the tuples in C# in my article here. One of the use of the tuple is to return the mutiple values from the methods.
    Tuple Before C# 7.0
            private Tuple<string, string> GetUserIDAndPwd()
            {
                return new Tuple<string, string>("UserID", "password");
            }
             
            private void AnotherMethod()
            {
                var keys = GetUserIDAndPwd();
                var userID = keys.Item1;
                var pwd = keys.Item2;
            }

    In the above code I am returning a tuple class which signifies the UserCredential class.

    In C# 7.0

    In C# 7.0 instead of using the Tuple we can  declare the tuple type while using with the function as shown in the code below.

            private (string UserName, string PassWord) GetUserIDAndPwd()
            {
                return ("Vikram", "Test123")
            }
             
            private void AnotherMethod()
            {
                var l1 = GetUserIDAndPwd();
                var userID = l1.UserName;
                var pwd = l1.Password;
            }

    Tuple deconstruction

    The above GetUserIDAndPwdCan be called as below also

            private void AnotherMethod()
            {
                (var usr, var pwd) = GetUserIDAndPwd();
                var userID = usr;
                var pwd = pwd;
            }
  2. Out Keyword –Till C# 7.0 – We have to declare the out variable before passing it as parameter to the function. More about the out and ref keywords you can read here.
    out keyword till C# 7.0
    public void GetName(Student s)
    {
        string firstName, lastName; // have to "predeclare"
        s.GetName(out firstName, out lastName);
        WriteLine($"({firstName}, {lastName})");
    }

    In C# 7.0
    No need to declare the out parameter. It can be declare at the place of calling the method. Please check the code below

    public void GetName(Student s)
    {    
        s.GetName(out string firstName, out string lastName);
        WriteLine($"({firstName}, {lastName})");
    }

    Since the compiler will know the type of the argument at the run time. We could have used var keyword for calling the GetName method.

  3. Local Functions – Though it is possible to declare the Func and Action methods till C# 6.0. But they lack the support for Generics, ref and out parameters and params for the parameters. In C# 7.0 we can declare the functions in local scope as shown in the code below
    public string GetName(int Id)  
    {  
       string Bar()  
       {  
         Console.WriteLine(“inner function”);  
       }  
       return Bar();  
    }

    Recursion and forward references would work for local functions but not for lambda. No memory allocation would be done if we use local functions. ref and out parameters are allowed. Params can be used for parameters and these local functions can be generic.

Conclusion:

In this article I have discussed about some of the new features of C# 7.0.

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview