Importing static methods from a namespace in C# 6

You’ll probably know that if you’d like to use the simple class names in a C# file you have to import its namespace in a using directive. If you want to use the Task class without referring to its fully qualified name including the namespace you have to put…

using System.Threading.Tasks;

…in the top section of the file. Then you can write something like…

Task t = Task.Run<ReturnType>(() => ...);

Similarly the using statement…

using System;

…is available by default in all new CS files in Visual Studio. The System namespace has a lot of members and one of them is the String class:

String startingPoint = "Hello world";

Checking whether a string is null or empty can be performed through a static method of the String class:

String.IsNullOrEmpty(startingPoint)

However, you cannot just refer to that static method by…

IsNullOrEmpty(startingPoint)

…even if you have imported the namespace where String is located. Let’s try the following:

using System.String;

We have no luck there of course, the compiler will complain:

A ‘using namespace’ directive can only be applied to namespaces; ‘string’ is a type not a namespace.

However, in C# 6 we can import the static members of a class as follows:

using static System.String;

Consider the following example where we use 3 static methods of String:

string startingPoint = "Hello world";
			
if (!String.IsNullOrEmpty(startingPoint))
{
	string toBePrinted = string.Format("I am saying {0}", string.Join(",", startingPoint, "some other string"));
	Console.WriteLine(toBePrinted);
}

With the ‘using static’ statement in place we can simplify it to the following:

string startingPoint = "Hello world";
			
if (!IsNullOrEmpty(startingPoint))
{
	string toBePrinted = Format("I am saying {0}", Join(",", startingPoint, "some other string"));
	Console.WriteLine(toBePrinted);
}

The compiler will now understand that you’re referring to the static methods of the String class.

Beware that if you have a method with the same signature available through a using statement or within the same class, e.g.:

private bool IsNullOrEmpty(string s)
{
	return false;
}

…then the compiler will refer to this method rather then String.IsNullOrEmpty.

Also, readers of the code may be wondering at first where the “Format” and “Join” methods are located. They certainly do not look like static methods just by looking at the code.

This feature is a neat addition to C# but be aware that it may cause some confusion.

View all various C# language feature related posts here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

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

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: