Wednesday, 4 November 2015

Features of C# 6.0 

There are following features.

1. Use of Static Members with Namespace
2. Auto Property Initialize


1. Use of Static Members with Namespace


We can use static classes in namespaces.

Example: 

a) Use of static class of system namespace.


using static System.Console;
using static System.Convert;

namespace CSharp6._0NewFeatures
{
    class Program
    {
        static void UsingStaticMember()
        {
            WriteLine("Use of System.Console\n\n");

            string age = "10";
            WriteLine("System.Convert: " + ToInt32(age));
        }

        static void Main(string[] args)
        {
            UsingStaticMember();
            ReadKey();
        }
    }
}

Output:
using static System.ClassName




















b) Use of static class of user namespace.


i)  Create a user define class

using System;
using static System.Console;

namespace CSharp6._0NewFeatures
{
    public static class Test
    {
        public static void UserMethod()
        {
            Write("User function.");
        }
    }
}

ii)  Use of user define class

using static System.Console;
using static CSharp6._0NewFeatures.Test;

namespace CSharp6._0NewFeatures
{
    class Program
    {
        static void Main(string[] args)
        {
            UserMethod();
            ReadKey();
        }
    }
}

Output


2. Auto Property Initialize



 It is very easy way to use property. We can initialize default value while declare property.

Example:

using static System.Console;

namespace CSharp6._0NewFeatures
{
    class Program
    {
        static string Name { get; set; } = "Arun Porwal";

        static void Main(string[] args)
        {
            WriteLine("Auto Property Initialize");
            Write("\n Default value :" + Name);

            Write("\n\nPlease enter name:");

            Name = ReadLine();

            Write("\n Run time value :" + Name);

            ReadKey();
        }
    }
}

Output:
static string Name { get; set; } = "Arun Porwal";





No comments:

Post a Comment