Monday 21 December 2015

Sample program in C# 6.0 features

In this post we are going to see a sample program in C# 6.0, by using almost all features of C# 6.0.

Samples are based on the concepts introduced newly, some of the two new concepts are not released in this version, let we see some concepts which are going to implement in this sample auto property initialize, string interpolation, conditional catch, new form of dictionary declaration, Null conditional operator for objects, Expression body function members.









using static System.Console;

namespace Samples
{
    public static class ExtensionMethods
    {
        public static string GetReverse(this string name)
        {
                return name.GetReverse();
        }
    
        public static void PrintWithStyle(string messgae)
        {
            WriteLine("********************************");
            WriteLine($"{messgae}");
            WriteLine("********************************\n");
        }

    }
}





using System;
using System.Collections.Generic;
using static System.Console;
using static Samples.ExtensionMethods;


namespace Samples
{

    class Person
    {

        public Person()
        {
            
        }

        /* Auto property intializers */
        public string FirstName { setget; } = "Rajesh";

        public string LastName { setget; } = "G";

        public int Age { setget; } = 15;

        /* Expression body function members */
        public string Address => "Chennai";

        /* Expression body function */
        public string GetFullName() => $"{FirstName} {LastName}";

        public int IncAge(int val) => val + Age;

        public override string ToString() => 
             $"{nameof(FirstName)} : {FirstName} /n {nameof(LastName)} :{LastName} ";
        

    }

    class Program
    {
        static void Main(string[] args)
        {

            Person employee = null;

            /* Null condition operator before object initialized */
            PrintWithStyle($"Checking Null : {employee?.FirstName ?? "Empty Object"}  ");
            employee?.GetFullName();

            /* Null condition operator after object initialized */
            employee = new Person();
            PrintWithStyle($"Checking Null : {employee?.FirstName}");
            employee?.GetFullName();

            /* Name of a Object */
            WriteLine(nameof(employee));

            /* New Way of Dictionary intializers */
            Dictionary<intPerson> emplist = new Dictionary<intPerson>()
            {
                [0] = new Person() { FirstName= "Suresh",LastName = "G",Age = 25},
                [1] = new Person() { FirstName = "Ramu", LastName = "D" ,Age = 3},
                [2] = new Person() { }
            };

            /* string interpolation */
            string format = $"{nameof(employee.FirstName)} : { employee.FirstName} /n 
                             {nameof(employee.LastName)} : {employee.LastName}";

            WriteLine(format);

            
            /* Conditional Catch operations */
            try
            {
                WriteLine($"Employee 1 : {emplist[0].FirstName}");
                employee = null;               
                throw new Exception("Sample error");
            }
            catch(Exception ex) when(employee==null)
            {
                WriteLine($"Employee is null {ex.Message}");
            }
            catch(Exception ex) when (employee !=null)
            {                
                WriteLine($"Error Message is : {ex.Message}");
            }

           
            Console.Read();

        }


    }

}



Output:
********************************
Checking Null : Empty Object
********************************

********************************
Checking Null : Rajesh
********************************

employee
FirstName : Rajesh /n LastName : G
Employee 1 : Suresh

Employee is null Sample error



From this post you can see the some sample program by using the C# 6.0 Features.




No comments:

Post a Comment