C# 6.0 Feature: Static Types as using



In previous post I pointed out the major changes in C#6.0.
From this post, I am taking changes one by one.

 Static Types as using

    You can use the static Members of class without qualifying their namespace or without
    type name.

    I'll write the codes form previous versions and later with version6.

    Here you can see the static classes like Console,Math etc., if you have to use it
    you have to specifically specify and call the methods in privious versions.But in
    version6  you can add static class as using and call the methods directly.

Before C#6

using System;

namespace NewInCSharp6
{
    class Program
    {
        static void Main(string[] args)
        {
            Self.MyName();

            Console.WriteLine(Math.Sqrt(10));

            Console.ReadKey();
        }
    }

    static class Self
    {
        public static void MyName()
        {
            Console.WriteLine("I'm kamal from techwalkin.com!");
        }
    }
}

After C#6









using System;

/* New feature*/
using static NewInCSharp6.Self;
using static System.Console;
using static System.Math;


namespace NewInCSharp6
{
    class Program
    {
        static void Main(string[] args)
        {
            // Calling the MyName method from Self static class.
            MyName();
         
            // calling the squareroot function from math clas.
            WriteLine(Sqrt(10));

            ReadKey();


        }
    }

    static class Self
    {
        public static void MyName()
        {
            WriteLine("I'm kamal from techwalkin!");
        }
    }
}


                                                                                                              Next: String Interpolation

C# 6.0 Feature: Static Types as using C# 6.0 Feature: Static Types as using Reviewed by kamal kumar das on November 24, 2016 Rating: 5

No comments:

ads 728x90 B
Powered by Blogger.