using keyword for static class in C# 6.0

C# 6.0 Features Series

In the earlier versions of C# , using keyword was used primarily for the namespaces to be included in the C# page. There were other usages of using keyword like using block , namespace alias etc.

The C# 6.0 lets the developers use the using directive for the static class similar to the one the developers use for namespace.

using keyword for static class in C# 6.0

This feature lets the developers to invoke the static methods of the class without specifying the class name.

Assume that you have a class called Console which has a static method called WriteLine . In C# 5.0 and earlier version , you would end up specifying Console.WriteLine(“developerpublish.com C# tutorials”); as show below.

using System;
using System.Collections.Generic;
using System.Linq;

namespace MobileOSGeekApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("developerpublish.com C# tutorials");

            Console.ReadLine();
        }
    }
}

In C# 6.0 , you can simply include the class name with the using class Eg : using System.Console; and when using the method , invoke it without the class name as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Console;
namespace MobileOSGeekApp
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("developerpublish.com C# tutorials");

            ReadLine();
        }
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...