Features in C# 5.0

Discussion in 'C#' started by MinalS, Mar 25, 2015.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    32
    Trophy Points:
    0
    The following new features are introduced in C# 5.0:
    1. Async and await
    2. Lambda expressions
    3. Caller Information
    4. Windows Runtime Support
    5. Compiler API

    1. Async and await



    In C# 5.0, two new keywords as async and await are introduced for asynchronous programming. The .NET Framework 4.5 and Windows Runtime are used to create the async methods. In 4.5 version, the task based asynchronous pattern ( TAP ) is used. The pattern uses the task type and helps the compiler use the compiler feature with the keywords.

    Async

    The keyword is useful for an asynchronous function. If the user specifies the async keyword before the function user needs to call the function asynchronously.
    The syntax for asynchronous method is as shown below:

    Code:
    public async void Process1()
    {
    }
    
    The Process1() method is declared as an asynchronous method. User can call the method asynchronously.

    Await

    The await keyword is useful when user calls the function asynchronously.
    Consider the following code to demonstrate the await keyword.

    Code:
    public static Task Process2()
    {
        return Task.Run( () => 
        {
            System.Threading.Thread.Sleep(7000);
        });
    }
    
    User can call Process2 asynchronously using the await keyword. The syntax is as mentioned below:
    Code:
    await Process2();
    
    Consider the following example to demonstrate the Windows application.
    Code:
    using System;
    using System.ComponentModel;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1: Form
        {
            InitializeComponent();
        }
        
        public static task Process2()
        {
            return Task.Run( () => 
            {
                System.Threading.Thread.Sleep(7000);
            });
        }
        
        public async void CallingProcess()
        {
            await. Process2();
            this.listbox1.Items.Add(“Process completed”);
        }
    
        private async void button1_Click(object sender, EventArgs e)
        {
            CallingProcess();
            this.listbox1.Items.Add(“Process completed”);
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
    
    In the above code, the CallingProcess() function is created that will wait for seven minutes. The function is declared as async keyword so that user can call it asynchronously.

    2. Lambda expressions



    In C#, lambda expressions are used when user has the delegate parameter type. The different syntax is used for assigning code implementation to the delegate.

    Consider the following code create using an anonymous method.
    Code:
    using System;
    namespace ConsoleAppication1
    {
        class Program
        {
            static void Main( string[ ] args )
            {
                string a = “, middle name,”;
            
            Func1<string, string> del1 = delegate ( string temp )
            {
                temp +=  a;
                temp += the value is added to name”;
                return temp;
            };
            Console.WriteLine(del1(“New String”));
            }
        }
    }
    
    In the above code, the delegate Func1 takes two string parameters and a string value is returned. The del1 variable is of delegate type. Using the anonymous method, the method is not defined. Only the delegate is used.

    The basic syntax of lambda expression is as shown below:
    Code:
    argument-list => expression
    
    Modify the above anonymous method using the lambda expression.
    Code:
    using System;
    
    namespace ConsoleAppication1
    {
        class Program
        {
            static void Main( string[ ] args )
            {
                string a = “, middle name,”;
            
            Func1<string, string> lambda = param = >
            {
                temp +=  a;
                temp += the value is added to name”;
                return temp;
            };
            Console.WriteLine(lambda(“New String”));
            }
        }
    }
    
    
    In the above code, the left side of the lambda operator, = > defines the parameter list to be used. The right side contains the implementation of the method.

    3. Caller Information



    The caller information attributes provides information about the caller to a method. User can access the file path, line number, and the member name of the source code. The information is useful in debugging, creating diagnostic tool and tracing.

    Three different attribute types are used in the caller information.
    1. CallerFilePath: It is used to set information about the caller source code file
    2. CallerMemberName: It is used to set the information about the caller member name
    3. CallerLineNumber: It is used to set the information about the line number of the caller
    The following code demonstrates the caller information.
    Code:
    using System;
    using System.Runtime.CompilerServices;
    
    public class Info
    {
        public static void ShowInfo( [ CallerMemberName ] string name = null,
        [ CallerLineNumber ] int line = -1,
        [ CallerFilePath ] string path = null )
        {
            Console.WriteLine(“Caller Name: {0}”, name );
            Console.WriteLine(“Caller FilePath: {0}”, path);
            Console.WriteLine(“Caller LineNumber : {0}”, line);
        }
    
        public static void Main ( )
        {
            Info();
            Console.ReadLine();
        }
    }
    

    4. Windows Runtime Support



    In Windows Runtime Support, the C# and .NET have good integration with it. The C# project is compiled in a WinMD file, are referenced from the JavaScript or HTML project.
    The WinRT of COM has the similar metadata format used in the Common Language Runtime. The WINMD file contain structure of the public classes. The runtime returns HRESULT and it does not throw an exception.

    5. Compiler API



    The API is used to expose the compiler knowledge about the code to the IDE. The code is exposed through syntax tree API, Binding and Flow Analysis API, Symbols API, and Emit API.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice