Tuesday 7 November 2017

using Static in C# 6.0

In this article, We will discuss How to use Static concept in C# 6.0.

Before 6.0, Whenever we were using any method like ToInt32(),ToString() etc then first of all we need to Write the Class Name and then Method Name. Where as those classes are Static only. So in C# 6.0 Microsoft made a change in behavior to it’s compiler. So in C# 6.0, We can declare all the Static Classes in NameSpace area and no need to again write the Same Class Name throughout the class and only required is that method name only.
For Example:
using System;

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("Hello");
            int val1 = ToInt32("78");
        }
    }
}

In the above Example I declared Convert and Console as static before the namespace and in the Main method directly we can Write ToInt32(),ToInt64() without Writing Convert each time. So due to this the line of code decreases but the output will be Same.

0 comments:

Post a Comment