Bhavya

Code -> Break -> Fix -> Blog

C# 6.0 : using Static Class

1 Comment

Problem : Using the Static class name over and over in the code to access it’s static method.

Prior to C# 6.0, to use a static method the developer also have to specify the Static Class name.


using System;

namespace MyNameSpace
{

internal class MyClass
{
static void Main(string[] args)
{
var inputName = Console.ReadLine();
Console.WriteLine("Hello " + inputName);

Console.ReadKey();
}
}
}

In the above code snippet, see the use of Console (static) class 3 times.

With C# 6.0, we can use the using keyword to refer to Console (static) class.


using System.Console;

namespace MyNameSpace
{

internal class MyClass
{
static void Main(string[] args)
{
var inputName = ReadLine();
WriteLine("Hello " + inputName);

ReadKey();
}
}
}

Prior to C# 6.0 the using keyword was used to refer only namespaces, like,


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

But with C# 6.0, we can use the using keyword to refer static class as well, like System.Console


using System.Console;

namespace MyNameSpace
{

internal class MyClass
{
static void Main(string[] args)
{
var inputName = ReadLine();
WriteLine("Hello " + inputName);

ReadKey();
}
}
}

New C# 6.0 features really help in writing clean code with the support of using keyword.

~BS

Twitter Logo

Advertisements

One thought on “C# 6.0 : using Static Class

  1. Pingback: New Features of C# 6.0 | Bhavya

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s